导包:
org.apache.httpcomponents
httpcore
4.4.6
org.apache.httpcomponents
httpclient
4.5.3
org.apache.httpcomponents
httpmime
4.5.2
1、构造只带URL的 HttpPost 请求:
HttpPost httpPost = new HttpPost(httpUrl);
2、构造请求客户端 httpClient 实例
(1)默认:
CloseableHttpClient httpClient = HttpClients.createDefault();
(2)自定义:
private static final RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(15000).setConnectTimeout(15000).setConnectionRequestTimeout(15000).build();
CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(requestConfig).build();
3、接受返回的响应请求
CloseableHttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
String responseContent = EntityUtils.toString(entity, "UTF-8");
1、构造HttpPost
(1)构造只带URL的 HttpPost 请求:
HttpPost httpPost = new HttpPost(httpUrl);
(2)设置 HttpPost 的 header :
httpPost.setHeader("Content-Type", "*/*");
/*
1)类型格式:type/subtype(;parameter)? type
2)主类型,任意的字符串,如text,如果是*号代表所有
3)subtype 子类型,任意的字符串,如html,如果是*号代表所有
4)parameter 可选,一些参数,如Accept请求头的q参数, Content-Type的 charset参数。
常见的Content-Type媒体格式类型如下:
text/html : HTML格式
text/plain :纯文本格式
text/xml : XML格式
image/gif :gif图片格式
image/jpeg :jpg图片格式
image/png:png图片格式
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 :
(3)设置 HttpPost 的 entity :
方式一:UrlEncodedFormEntity 会将参数以key1=value1&key2=value2的键值对形式发出。
param参数以List
//直接将键值对一个一个写入List
List pairs = new ArrayList();
NameValuePair pair1 = new BasicNameValuePair("supervisor", supervisorEt.getEditableText().toString());
NameValuePair pair2 = new BasicNameValuePair("content", superviseContentEt.getEditableText().toString());
NameValuePair pair3 = new BasicNameValuePair("userId", String.valueOf(signedUser.getId()));
pairs.add(pair1);
pairs.add(pair2);
pairs.add(pair3);
httpPost.setEntity(new UrlEncodedFormEntity(pairs, HTTP.UTF_8));
//将Map转化为List
List nameValuePairs = new ArrayList<>();
for (String key : maps.keySet()) {
//Map类型参数写进NameValuePair类型的URL参数中去
nameValuePairs.add(new BasicNameValuePair(key, maps.get(key)));
}
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
方式二:StringEntity tringEntity 可以自己指定ContentType,而默认值是 text/plain。所以不指定ContentType的话 不是以键值对形式发出,如果服务器只接收键值对的话就无法正常解析;
param参数可以为"key1=value1&key2=value2"的一串字符串,或者是jsonObject
StringEntity stringEntity1 = null ;
//字符串
String param = "key1=value1&key2=value2";
stringEntity1 = new StringEntity(param);
//jsonObject
StringEntity stringEntity2 = null ;
JSONObject postData = new JSONObject();
postData.put("supervisor", supervisorEt.getEditableText().toString()); postData.put("content", superviseContentEt.getEditableText().toString()); postData.put("userId", signedUser.getId());
stringEntity2 = new StringEntity(postData.toString(), HTTP.UTF_8);
stringEntity的ContentType设置(详见:常见的Content-Type媒体格式类型) stringEntity1.setContentType("application/json");
stringEntity设置完毕
httpPost.setEntity(stringEntity1);
2、构造请求客户端 httpClient 实例
(1)默认:
CloseableHttpClient httpClient = HttpClients.createDefault();
(2)自定义:
private static final RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(15000).setConnectTimeout(15000).setConnectionRequestTimeout(15000).build();
CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(requestConfig).build();
3、接受返回的响应请求
CloseableHttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
String responseContent = EntityUtils.toString(entity, "UTF-8");
底线