HttpClient工具类及应用
Content-Type类型:
常见的媒体格式类型如下:
- text/html : HTML格式
- text/plain :纯文本格式
- text/xml : XML格式
- image/gif :gif图片格式
- image/jpeg :jpg图片格式
- image/png:png图片格式
以application开头的媒体格式类型:
application/xhtml+xml :XHTML格式
application/xml : XML数据格式
application/atom+xml :Atom XML聚合格式
application/json : JSON数据格式
application/pdf :pdf格式
application/msword : Word文档格式
application/octet-stream : 二进制流数据(如常见的文件下载)
application/x-www-form-urlencoded : 中默认的encType,form表单数据被编码为key/value格式发送到服务器(表单默认的提交数据的格式)
另外一种常见的媒体格式是上传文件之时使用的:multipart/form-data : 需要在表单中进行文件上传时,就需要使用该格式
区别:
application/xml 媒体类型:推荐使用。如果 MIME 用户代理或 Web 用户代理不支持这个媒体类型,会转为 application/octet-stream,当做二进制流来处理。application/xml 实体默认用 UTF-8 字符集。Content-type: application/xml; charset=“utf-8” 或 <?xml version="1.0" encoding="utf-8"?> 都可以生效。
text/xml 媒体类型:如果 MIME 用户代理或 Web 用户代理不支持这个媒体类型,会将其视为 text/plain,当做纯文本处理。text/xml 媒体类型限制了 XML 实体中可用的编码类型(例如此时支持 UTF-8 但不支持 UTF-16,因为使用 UTF-16 编码的文本在处理 CR,LF 和 NUL 会导致异常转换)。text/xml 实体在 XML 头指定编码格式无效,必须在 HTTP 头部的 Content-Type: 中指定才会生效(例如 <?xml version="1.0" encoding="utf-8"?> 无法设置字符集,Content-Type: text/xml; charset=“utf-8” 则可以)。没有设置字符集时默认使用“us-ascii”字符集。
package com.xiaobu.base.utils;import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;/*** Content-type 常用的几种类型* application/x-www-form-urlencoded :form表单键值对<K,V>类型 默认类型** @author xiaobu* @version JDK1.8.0_171* @date on 2018/12/10 10:40* @description V1.0 HttpClient工具类*/
public class HttpClientUtils {/*** 获取httppost对象 设置连接超时属性* setConnectTimeout:设置连接超时时间,单位毫秒。* setConnectionRequestTimeout:设置从connect Manager获取Connection 超时时间,单位毫秒。这个属性是新加的属性,因为目前版本是可以共享连接池的。* setSocketTimeout:请求获取数据的超时时间,单位毫秒。 如果访问一个接口,多少时间内无法返回数据,就直接放弃此次调用。*/public static HttpPost getHttpPost(String url) {// 创建post方式请求对象HttpPost httpPost = new HttpPost(url);RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(5000).setConnectionRequestTimeout(1000).setSocketTimeout(5000).build();httpPost.setConfig(requestConfig);return httpPost;}/*** @param url 请求地址, map 数据类型, encoding 编码]* @return java.lang.String* @author xiaobu* @date 2018/12/10 10:46* @descprition pots请求传输 形式数据形式访问* @version 1.0*/public static String sendPostDataByMap(String url, Map<String, String> map, String encoding) {// 创建httpclient对象CloseableHttpClient httpClient = HttpClients.createDefault();// 创建post方式请求对象HttpPost httpPost = getHttpPost(url);// 装填参数List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();if (map != null) {for (Map.Entry<String, String> entry : map.entrySet()) {nameValuePairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));}}// 设置参数到请求对象中try {httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, encoding));} catch (UnsupportedEncodingException e) {e.printStackTrace();}// 设置header信息// 指定报文头【Content-type】、【User-Agent】httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");httpPost.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");CloseableHttpResponse response = getPostResponse(httpClient, httpPost);return getResult(response, encoding);}/*** @param url 请求笛子, json 请求数据类型, encoding编码* @return java.lang.String* @author xiaobu* @date 2018/12/10 10:45* @descprition post请求传输json* @version 1.0* JSON.toJSONString(map) 将map对象转化为json字符串*/public static String sendPostDataByJson(String url, String json, String encoding) {// 创建httpclient对象CloseableHttpClient httpClient = HttpClients.createDefault();// 创建post方式请求对象HttpPost httpPost = getHttpPost(url);// 设置参数到请求对象中StringEntity stringEntity = new StringEntity(json, ContentType.APPLICATION_JSON);stringEntity.setContentEncoding(encoding);httpPost.setEntity(stringEntity);CloseableHttpResponse response = getPostResponse(httpClient, httpPost);return getResult(response, encoding);}public static String sendPostDataByXml(String url, String xml, String encoding) {// 创建httpclient对象CloseableHttpClient httpClient = HttpClients.createDefault();// 创建post方式请求对象HttpPost httpPost = getHttpPost(url);httpPost.addHeader("Content-Type", "text/xml;charset=UTF-8");// 设置参数到请求对象中 text/xml和application/xml的区别StringEntity stringEntity = new StringEntity(xml, encoding);stringEntity.setContentEncoding(encoding);httpPost.setEntity(stringEntity);CloseableHttpResponse response = getPostResponse(httpClient, httpPost);return getResult(response, encoding);}/*** @param url 访问地址, encoding 编码]* @return java.lang.String* @author xiaobu* @date 2018/12/10 11:17* @descprition get方式请求* setConnectTimeout:设置连接超时时间,单位毫秒。* setConnectionRequestTimeout:设置从connect Manager获取Connection 超时时间,单位毫秒。这个属性是新加的属性,因为目前版本是可以共享连接池的。* setSocketTimeout:请求获取数据的超时时间,单位毫秒。 如果访问一个接口,多少时间内无法返回数据,就直接放弃此次调用。* @version 1.0*/public static String sendGetData(String url, String encoding) {// 创建httpclient对象CloseableHttpClient httpClient = HttpClients.createDefault();// 创建get方式请求对象HttpGet httpGet = new HttpGet(url);RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(5000).setConnectionRequestTimeout(1000).setSocketTimeout(5000).build();httpGet.setConfig(requestConfig);// Content-type设置为application/x-www-form-urlencoded 或者不设置也是可以的(默认为application/x-www-form-urlencoded)httpGet.addHeader("Content-type", "application/json");// 通过请求对象获取响应对象CloseableHttpResponse response = getGetResponse(httpClient, httpGet);return getResult(response, encoding);}/*** @param httpClient , httpPost* @return org.apache.http.client.methods.CloseableHttpResponse* @author xiaobu* @date 2018/12/10 11:18* @descprition 获取response对象* @version 1.0*/public static CloseableHttpResponse getPostResponse(CloseableHttpClient httpClient, HttpPost httpPost) {CloseableHttpResponse response = null;try {response = httpClient.execute(httpPost);} catch (IOException e) {e.printStackTrace();}return response;}/*** @param httpClient , httpGET* @return org.apache.http.client.methods.CloseableHttpResponse* @author xiaobu* @date 2018/12/10 11:18* @descprition 获取response对象* @version 1.0*/public static CloseableHttpResponse getGetResponse(CloseableHttpClient httpClient, HttpGet httpGet) {CloseableHttpResponse response = null;try {response = httpClient.execute(httpGet);} catch (IOException e) {e.printStackTrace();}return response;}/*** @param response, encoding]* @return java.lang.String* @author xiaobu* @date 2018/12/10 11:18* @descprition 获取结果* @version 1.0*/public static String getResult(CloseableHttpResponse response, String encoding) {String result = "";if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {try {result = EntityUtils.toString(response.getEntity(), encoding);} catch (IOException e) {e.printStackTrace();}}// 释放链接try {response.close();} catch (IOException e) {e.printStackTrace();}return result;}}
服务端代码:
/*** 功能描述: Post方式获取数据* @author xiaobu* @date 2019/8/23 12:36* @param data 用于获取application/x-www-form-urlencoded 下的参数名称data的数据 , request 通过流来获取body的数据* @return java.lang.String* @version 1.0*/@PostMapping("revQrCodeRelationData")public String revQrCodeRelationData(String data, HttpServletRequest request) {try {InputStream inputStream = request.getInputStream();BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));String line = null;StringBuilder stringBuilder = new StringBuilder();while ((line = bufferedReader.readLine()) != null) {//读取并换行stringBuilder.append(line + "\n");}System.out.println("stringBuilder = " + stringBuilder);} catch (IOException e) {e.printStackTrace();}return null;}/*** 功能描述:Get方式* @author xiaobu* @date 2019/8/23 12:41* @param Sign, timeStamp, device, quntity* @return java.lang.String* @version 1.0*/@GetMapping("sendQRCodeData")public String sendQrCodeData(String Sign,long timeStamp,String device,int quntity){Map<String, String> data = new HashMap<>();data.put("QRCode1", "http://xxxxxxx1");data.put("QRCode2", "http://xxxxxxx2");data.put("QRCode3", "http://xxxxxxx3");data.put("IndexCode1", "001");data.put("IndexCode2", "002");data.put("IndexCode3", "003");data.put("Quantity", quntity+"");return "success";}
测试代码:
package com.xiaobu;import com.xiaobu.base.utils.HttpClientUtils;import java.util.HashMap;
import java.util.Map;/*** @author xiaobu* @version JDK1.8.0_171* @date on 2018/12/27 15:55* @description V1.0*/
public class HttpClientDemo {public static void main(String[] args) {String postUrl = "http://localhost:8899/api/revQrCodeRelationData";//通过K-V来获取 SpringMVC实现了封装Map<String, String> map = new HashMap<>();map.put("data", "我是你大爷");String result = HttpClientUtils.sendPostDataByMap(postUrl, map, "UTF-8");System.out.println("result = " + result);//通过request.getInputStream()来获取String json = "我是你二大爷";String jsonResult=HttpClientUtils.sendPostDataByJson(postUrl, json, "UTF-8");System.out.println("jsonResult = " + jsonResult);//通过request.getInputStream()来获取String xml = "<!--?xml version=\"1.0\"?-->\n" +"<methodcall>\n" +" <methodname>examples.getStateName</methodname>\n" +" <params>\n" +" <param>\n" +" <value><i4>中国人</i4></value>\n" +"\n" +" </params>\n" +"</methodcall>";String xmlResult=HttpClientUtils.sendPostDataByXml(postUrl, xml, "UTF-8");System.out.println("xmlResult = " + xmlResult);//get方式String getUrl = "http://localhost:8899/api/sendQRCodeData?Sign=1&timeStamp=1234567890123&device=1&quntity=1";String getResult = HttpClientUtils.sendGetData(getUrl, "UTF-8");System.out.println("getResult = " + getResult);}
}
参考:
Http中Content-Type的详解
HttpClient工具类及应用相关推荐
- Java开发小技巧(五):HttpClient工具类
前言 大多数Java应用程序都会通过HTTP协议来调用接口访问各种网络资源,JDK也提供了相应的HTTP工具包,但是使用起来不够方便灵活,所以我们可以利用Apache的HttpClient来封装一个具 ...
- HttpClient工具类
HttpClient工具类 package cn.sh.steven.httpclient;import com.alibaba.fastjson.JSON; import com.alibaba.f ...
- apache httpclient 工具类_HttpClient
HttpClient 简介 HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的.最新的.功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 ...
- apache httpclient 工具类_HttpClient 和Mycat 主从节点和读写分离
第175次(HttpClient) 学习主题:HttpClient 学习目标: 1 掌握HttpClient自定义工具以及HttpClient具体的使用 对应视频: http://www.itbaiz ...
- apache httpclient 工具类_使用HttpClient进行服务的远程调用
目标:使用apache公司的产品http httpcomponents 完成服务调用. HTTPClient调用服务 4:导入httpclient的依赖配置 org.apache.httpcompon ...
- 使用单例模式实现自己的HttpClient工具类
本文转载自:http://www.cnblogs.com/codingmyworld/archive/2011/08/17/2141706.html 使用单例模式实现自己的HttpClient工具类 ...
- 14、阿里云短信Demo演示、Http的Get请求和Post请求演示、httpClient工具类演示、发送短信模块搭建、搭建用户中心模块、完成user注册基本功能、验证码存入redis、短信验证码注册
阿里云短信Demo演示 一.前端部分 无前端. 二.后端部分 1.创建发送短信测试模块SmsSendDemo,不用使用骨架. 2.在pom文件中引入依赖坐标 <dependency>< ...
- 工具类-httpClient工具类
httpClient工具类 1.httpClient工具类(http/https.重发.超时.连接数的设置) package com.xxxxxxx.xxxx.xxxx.payutil;import ...
- HttpClient工具类封装
HttpClient是Apache Jakarta Common下的子项目,用来提供高效的.最新的.功能丰富的支持HTTP协议的客户端编程工具包,并且它支持HTTP协议最新的版本和建议.HttpCli ...
最新文章
- ECLIPSE启动不了,报错org.eclipse.swt.SWTException: Invalid thread access
- linux之tr命令详解
- shell脚本复制文件夹内容到另外的文件夹,如果存在则自动备份
- 药理学css概念,药理学css 名词解释
- BlockJUnit4ClassRunner
- SQL Server 急救包(First Responder Kit)入门教程
- CVPR 9999 Best Paper——《一种加辣椒的番茄炒蛋》
- 随想录(关于dsp)
- 16进制字符串转字节数组
- Linux的动态库和静态库
- java铁三公路自行车_公路车、计时车、铁三车,到底都是什么鬼?
- 【PCI】PCI驱动匹配(四)
- 离散 单射 满射 双射
- 墨迹天气3.0引导动画
- 语音信号的梅尔频率倒谱系数(MFCC)的原理讲解及python实现
- 计算机学院迎新晚会主题八个字,大学迎新晚会主题
- python遥控汽车玩具_遥控小车
- 2019暑期集训收获感悟
- 乐鑫Esp32学习之旅⑦ esp32上利用GPIO中断做一个按键的短按和长按的回调事件,再也无须担心触发源。(附带Demo)
- Android Studio底部导航
热门文章
- 【python和机器学习入门3】朴素贝叶斯1——过滤恶意留言
- OPPO R6007线刷刷机包 救砖解锁 刷机教程
- 6月3日 历史上的大事记
- 【181207】C++工资管理系统(Access)源代码
- 手把手教你:jsp中无法使用My97DatePicker的解决方法
- 谈谈Q+平台的技术实现
- 为什么NaN===NaN?
- python怎么爬虫人信息_Python爬虫实现全国失信被执行人名单查询功能示例
- string 实现字符串分割
- 云图“漫漫”,新华三云学堂2.0从校园走向城域