maven
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.1</version>
<scope>compile</scope>
</dependency>
请求头 content-type 类型 https://www.runoob.com/http/http-content-type.html
multipart/form-data : 需要在表单中进行文件上传时,就需要使用该格式
请求头 Accept代表发送端(客户端)希望接受的数据类型。
使用
public static String post(String url, String jsonstr, Map<String, String> heads) throws IOException {
String result = "";
CloseableHttpResponse httpResponse = null;
CloseableHttpClient httpClient = null;
HttpPost httpPost = null;
try {
// 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的)
httpClient = HttpClientBuilder.create().build();
String charset = "utf-8";
// 定义请求的参数
//URI uri = new URIBuilder("http://www.baidu.com/s").setParameter("wd", "java").build();
//httpGet = new HttpGet(uri);
// 创建Post请求
httpPost = new HttpPost(url);
//解析对象
StringEntity entity = new StringEntity(jsonstr, charset);
httpPost.setEntity(entity);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
if (heads != null) {
for (String headKey : heads.keySet()) {
httpPost.setHeader(headKey, heads.get(headKey));
}
}
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(1000) //连接超时时间
.setConnectionRequestTimeout(1000) //从连接池中取的连接的最长时间
.setSocketTimeout(10 *1000) //数据传输的超时时间
.build();
httpResponse = httpClient.execute(httpPost);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
result = EntityUtils.toString(httpResponse.getEntity(), charset);
}
result = httpResponse.toString();
}finally {
if(httpResponse != null){
try {
httpResponse.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
if(httpPost != null){
httpPost.clone();
}
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
if(httpClient != null){
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
配置API RequestConfig()
protected RequestConfig |
clone() |
static RequestConfig.Builder |
copy(RequestConfig config) |
static RequestConfig.Builder |
custom() |
int |
getConnectionRequestTimeout() 返回从连接管理器请求连接时使用的超时(以毫秒为单位)。 |
int |
getConnectTimeout() 确定连接建立之前的超时时间(以毫秒为单位)。 |
String |
getCookieSpec() 确定用于HTTP状态管理的cookie规范的名称。 |
InetAddress |
getLocalAddress() 返回用于执行请求的本地地址。 |
int |
getMaxRedirects() 返回要遵循的最大重定向数。 |
org.apache.http.HttpHost |
getProxy() 返回用于请求执行的HTTP代理。 |
Collection<String> |
getProxyPreferredAuthSchemes() 确定使用代理主机进行身份验证时支持的身份验证方案的优先顺序。 |
int |
getSocketTimeout() 定义套接字超时( SO_TIMEOUT ),以毫秒为单位,这是等待数据的超时,或者换句话说,是两个连续数据包之间的最大时间段不活动。 |
Collection<String> |
getTargetPreferredAuthSchemes() 确定使用目标主机进行身份验证时支持的身份验证方案的优先顺序。 |
boolean |
isAuthenticationEnabled() 确定是否应自动处理身份验证。 |
boolean |
isCircularRedirectsAllowed() 确定是否应允许循环重定向(重定向到同一位置)。 |
boolean |
isContentCompressionEnabled() 确定是否请求目标服务器压缩内容。 |
boolean |
isDecompressionEnabled() 不推荐使用。 (4.5)使用 isContentCompressionEnabled() |
boolean |
isExpectContinueEnabled() 确定是否为实体封装方法启用了“期望:100-连续”握手。 |
boolean |
isNormalizeUri() 确定客户端是否应规范请求中的URI。 |
boolean |
isRedirectsEnabled() 确定是否应自动处理重定向。 |
boolean |
isRelativeRedirectsAllowed() 确定是否应拒绝相对重定向。 |
boolean |
isStaleConnectionCheckEnabled() 不推荐使用。 (4.4)使用 PoolingHttpClientConnectionManager.getValidateAfterInactivity() |
String |
toString() |