本文为博主原创,未经允许不得转载:

  在项目中会用到各种类型的http请求,包含put,get,post,delete,formData等各种请求方式,在这里总结一下

用过比较好的请求工具,使用service方法封装。

  代码如下:

1.依赖的maven

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>

2.封装请求返回的实体类

@Setter
@Getter
public class ApacheHttpClientResult { /** 状态码 **/
private int statusCode; /** 响应内容 **/
private String responseContent;
}

3.封装各类型请求的方法

import java.util.Map;
import org.apache.http.entity.mime.MultipartEntityBuilder; import com.vo.ApacheHttpClientResult; public interface ApacheHttpClientService { public ApacheHttpClientResult postForJson(String uri, String param) throws CustomException; public ApacheHttpClientResult postForJson(String uri, MultipartEntityBuilder param)throws CustomException; public ApacheHttpClientResult getForObject(String uri)throws CustomException; public ApacheHttpClientResult putForJson(String uri, String param)throws CustomException; public ApacheHttpClientResult deleteForJson(String uri, String param)throws CustomException; public ApacheHttpClientResult postForJson(String uri, String param, Map<String, String> headers)throws CustomException; public ApacheHttpClientResult getForObject(String uri, Map<String, String> headers)throws CustomException; public ApacheHttpClientResult putForJson(String uri, String param, Map<String, String> headers)throws CustomException; public ApacheHttpClientResult deleteForJson(String uri, String param, Map<String, String> headers)throws CustomException; public ApacheHttpClientResult getForObjectCloud(String uri, Map<String, String> headers) throws CustomException; public ApacheHttpClientResult getForObjectCloud(String uri) throws CustomException; public ApacheHttpClientResult postForJsonNoProxy(String uri, String param) throws CustomException; public ApacheHttpClientResult postForJson(String uri, String param, Map<String, String> headers, Map<String, String> resultHeaders); public ApacheHttpClientResult putForJson(String uri, String param, Map<String, String> headers, Map<String, String> resultHeaders); }

4.实现类

import java.io.Closeable;
import java.io.IOException;
import java.net.Proxy;
import java.util.Map; import org.apache.commons.lang3.StringUtils;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.config.RequestConfig.Builder;
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.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service; import com.common.CustomException;
import com.common.HttpDeleteWithBody;
import com.intf.service.common.ApacheHttpClientService;
import com.vo.ApacheHttpClientResult; @Service("apacheHttpClientService12")
public class Test implements ApacheHttpClientService{ private final static Boolean enabled = false; private final static String host = "127.0.0.1"; private final static Integer port = 8080; private final static int timeOut=20000; private final static Boolean proxyEnabled= false; private static final Logger LOGGER = LoggerFactory.getLogger(ApacheHttpClientServiceImpl.class); /**
*
* 功能描述: <br>
* 创建默认Builder
*
* @return
* @see [相关类/方法](可选)
* @since [产品/模块版本](可选)
*/
private Builder createBuilder() {
// init Builder and init TIME_OUT
return RequestConfig.custom().setSocketTimeout(timeOut).setConnectTimeout(timeOut)
.setConnectionRequestTimeout(timeOut);
} @Override
public ApacheHttpClientResult postForJson(String uri, String param) throws CustomException {
// 定义返回
ApacheHttpClientResult result = new ApacheHttpClientResult();
// 定义httpClient和response
CloseableHttpClient httpClient = HttpClientBuilder.create().build();; CloseableHttpResponse response = null;
try {
// 定义Post请求
HttpPost httpPost = new HttpPost(uri);
// 设置配置
Builder builder = createBuilder();
RequestConfig config = null;
// 是否开启代理模式访问
if (enabled) {
HttpHost proxy = new HttpHost(host, port, Proxy.Type.HTTP.toString());
config = builder.setProxy(proxy).build();
} else {
config = builder.build();
}
httpPost.setConfig(config);
// 设置请求头
httpPost.setHeader("Accept", MediaType.APPLICATION_JSON_VALUE);
httpPost.setHeader("Content-Type", MediaType.APPLICATION_JSON_UTF8_VALUE);
// 发送请求得到返回数据
httpPost.setEntity(new StringEntity(param, "UTF-8"));
// 得到响应
response = httpClient.execute(httpPost);
// 状态码
result.setStatusCode(response.getStatusLine().getStatusCode());
// 响应内容
HttpEntity entity = response.getEntity();
// 响应内容
String responseContent = EntityUtils.toString(entity);
result.setResponseContent(responseContent);
} catch (Exception e) {
throw new CustomException(e);
} finally {
// 关闭流
closeStream(response);
closeStream(httpClient);
}
return result;
} @Override
public ApacheHttpClientResult getForObject(String uri) throws CustomException {
// 定义返回
ApacheHttpClientResult result = new ApacheHttpClientResult();
// 定义httpClient和response
CloseableHttpClient httpClient = HttpClientBuilder.create().build();;
CloseableHttpResponse response = null;
try {
// HttpGet
HttpGet httpGet = new HttpGet(uri);
// 设置配置
Builder builder = createBuilder();
RequestConfig config = null;
// 是否开启代理模式访问
if (enabled) {
HttpHost proxy = new HttpHost(host, port, Proxy.Type.HTTP.toString());
config = builder.setProxy(proxy).build();
} else {
config = builder.build();
}
httpGet.setConfig(config);
// 发送请求得到返回数据
response = httpClient.execute(httpGet);
// 状态码
result.setStatusCode(response.getStatusLine().getStatusCode());
// 响应内容
HttpEntity entity = response.getEntity();
// 响应内容
String responseContent = EntityUtils.toString(entity, "UTF-8");
result.setResponseContent(responseContent);
} catch (Exception e) {
throw new CustomException(e);
} finally {
// 关闭流
closeStream(response);
closeStream(httpClient);
}
return result;
} @Override
public ApacheHttpClientResult putForJson(String uri, String param) throws CustomException {
// 定义返回
ApacheHttpClientResult result = new ApacheHttpClientResult();
// 定义httpClient和response
CloseableHttpClient httpClient = HttpClientBuilder.create().build();;
CloseableHttpResponse response = null;
try {
// HttpPut
HttpPut httpPut = new HttpPut(uri);
// 设置配置
Builder builder = createBuilder();
RequestConfig config = null;
// 是否开启代理模式访问
if (enabled) {
HttpHost proxy = new HttpHost(host, port, Proxy.Type.HTTP.toString());
config = builder.setProxy(proxy).build();
} else {
config = builder.build();
}
httpPut.setConfig(config);
// 设置请求头
httpPut.setHeader("Accept", MediaType.APPLICATION_JSON_VALUE);
httpPut.setHeader("Content-Type", MediaType.APPLICATION_JSON_UTF8_VALUE);
// 发送请求得到返回数据
httpPut.setEntity(new StringEntity(param, "UTF-8"));
// 得到响应
response = httpClient.execute(httpPut);
// 状态码
result.setStatusCode(response.getStatusLine().getStatusCode());
// 响应内容
HttpEntity entity = response.getEntity();
// 响应内容
String responseContent = EntityUtils.toString(entity);
result.setResponseContent(responseContent);
} catch (Exception e) {
throw new CustomException(e);
} finally {
// 关闭流
closeStream(response);
closeStream(httpClient);
}
return result;
} @Override
public ApacheHttpClientResult deleteForJson(String uri, String param) throws CustomException {
// 定义返回
ApacheHttpClientResult result = new ApacheHttpClientResult();
// 定义httpClient和response
CloseableHttpClient httpClient = HttpClientBuilder.create().build();;
CloseableHttpResponse response = null;
try {
// HttpDelete
HttpDeleteWithBody httpDelete = new HttpDeleteWithBody(uri);
// 设置配置
Builder builder = createBuilder();
RequestConfig config = null;
// 是否开启代理模式访问
if (enabled) {
HttpHost proxy = new HttpHost(host, port, Proxy.Type.HTTP.toString());
config = builder.setProxy(proxy).build();
} else {
config = builder.build();
}
httpDelete.setConfig(config);
// 设置请求头
httpDelete.setHeader("Accept", MediaType.APPLICATION_JSON_VALUE);
httpDelete.setHeader("Content-Type", MediaType.APPLICATION_JSON_UTF8_VALUE);
// 发送请求得到返回数据
httpDelete.setEntity(new StringEntity(param, "UTF-8"));
// 得到响应
response = httpClient.execute(httpDelete);
// 状态码
result.setStatusCode(response.getStatusLine().getStatusCode());
// 响应内容
HttpEntity entity = response.getEntity();
// 响应内容
String responseContent = EntityUtils.toString(entity);
result.setResponseContent(responseContent);
} catch (Exception e) {
throw new CustomException(e);
} finally {
// 关闭流
closeStream(response);
closeStream(httpClient);
}
return result;
} @Override
public ApacheHttpClientResult postForJson(String uri, String param, Map<String, String> headers)
throws CustomException {
// 定义返回
ApacheHttpClientResult result = new ApacheHttpClientResult();
// 定义httpClient和response
CloseableHttpClient httpClient = HttpClientBuilder.create().build();;
CloseableHttpResponse response = null;
try {
// 定义Post请求
HttpPost httpPost = new HttpPost(uri);
// 设置配置
Builder builder = createBuilder();
RequestConfig config = null;
// 是否开启代理模式访问
if (enabled) {
HttpHost proxy = new HttpHost(host, port, Proxy.Type.HTTP.toString());
config = builder.setProxy(proxy).build();
} else {
config = builder.build();
}
httpPost.setConfig(config);
// 设置请求头
httpPost.setHeader("Accept", MediaType.APPLICATION_JSON_VALUE);
httpPost.setHeader("Content-Type", MediaType.APPLICATION_JSON_UTF8_VALUE);
if (headers != null) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
httpPost.setHeader(entry.getKey(), entry.getValue());
}
}
// 发送请求得到返回数据
httpPost.setEntity(new StringEntity(param, "UTF-8"));
// 得到响应
response = httpClient.execute(httpPost);
// 状态码
result.setStatusCode(response.getStatusLine().getStatusCode());
// 响应内容
HttpEntity entity = response.getEntity();
// 响应内容
String responseContent = EntityUtils.toString(entity);
result.setResponseContent(responseContent);
} catch (Exception e) {
throw new CustomException(e);
} finally {
// 关闭流
closeStream(response);
closeStream(httpClient);
}
return result;
} @Override
public ApacheHttpClientResult getForObject(String uri, Map<String, String> headers) throws CustomException {
// 定义返回
ApacheHttpClientResult result = new ApacheHttpClientResult();
// 定义httpClient和response
CloseableHttpClient httpClient = HttpClientBuilder.create().build();;
CloseableHttpResponse response = null;
try {
// HttpGet
HttpGet httpGet = new HttpGet(uri);
// 设置配置
Builder builder = createBuilder();
RequestConfig config = null;
// 是否开启代理模式访问
if (enabled) {
HttpHost proxy = new HttpHost(host, port, Proxy.Type.HTTP.toString());
config = builder.setProxy(proxy).build();
} else {
config = builder.build();
}
httpGet.setConfig(config);
// 设置请求头
if (headers != null) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
httpGet.setHeader(entry.getKey(), entry.getValue());
}
}
// 发送请求得到返回数据
response = httpClient.execute(httpGet);
// 状态码
result.setStatusCode(response.getStatusLine().getStatusCode());
// 响应内容
HttpEntity entity = response.getEntity();
// 响应内容
String responseContent = EntityUtils.toString(entity);
result.setResponseContent(responseContent);
LOGGER.info(uri + "&&&&&" + response.toString() + "&&&&&" + responseContent);
} catch (Exception e) {
throw new CustomException(e);
} finally {
// 关闭流
closeStream(response);
closeStream(httpClient);
}
return result;
} @Override
public ApacheHttpClientResult putForJson(String uri, String param, Map<String, String> headers)
throws CustomException {
// 定义返回
ApacheHttpClientResult result = new ApacheHttpClientResult();
// 定义httpClient和response
CloseableHttpClient httpClient = HttpClientBuilder.create().build();;
CloseableHttpResponse response = null;
try {
// HttpPut
HttpPut httpPut = new HttpPut(uri);
// 设置配置
Builder builder = createBuilder();
RequestConfig config = null;
// 是否开启代理模式访问
if (enabled) {
HttpHost proxy = new HttpHost(host, port, Proxy.Type.HTTP.toString());
config = builder.setProxy(proxy).build();
} else {
config = builder.build();
}
httpPut.setConfig(config);
// 设置请求头
httpPut.setHeader("Accept", MediaType.APPLICATION_JSON_VALUE);
httpPut.setHeader("Content-Type", MediaType.APPLICATION_JSON_UTF8_VALUE);
if (headers != null) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
httpPut.setHeader(entry.getKey(), entry.getValue());
}
}
// 发送请求得到返回数据
httpPut.setEntity(new StringEntity(param, "UTF-8"));
// 得到响应
response = httpClient.execute(httpPut);
// 状态码
result.setStatusCode(response.getStatusLine().getStatusCode());
// 响应内容
HttpEntity entity = response.getEntity();
// 响应内容
String responseContent = EntityUtils.toString(entity);
result.setResponseContent(responseContent);
LOGGER.info(uri + "&&&&&" + response.toString() + "&&&&&" + param);
} catch (Exception e) {
throw new CustomException(e);
} finally {
// 关闭流
closeStream(response);
closeStream(httpClient);
}
return result;
} @Override
public ApacheHttpClientResult deleteForJson(String uri, String param, Map<String, String> headers)
throws CustomException {
// 定义返回
ApacheHttpClientResult result = new ApacheHttpClientResult();
// 定义httpClient和response
CloseableHttpClient httpClient = HttpClientBuilder.create().build();;
CloseableHttpResponse response = null;
try {
// HttpDelete
HttpDeleteWithBody httpDelete = new HttpDeleteWithBody(uri);
// 设置配置
Builder builder = createBuilder();
RequestConfig config = null;
// 是否开启代理模式访问
if (enabled) {
HttpHost proxy = new HttpHost(host, port, Proxy.Type.HTTP.toString());
config = builder.setProxy(proxy).build();
} else {
config = builder.build();
}
httpDelete.setConfig(config);
// 设置请求头
httpDelete.setHeader("Accept", MediaType.APPLICATION_JSON_VALUE);
httpDelete.setHeader("Content-Type", MediaType.APPLICATION_JSON_UTF8_VALUE);
if (headers != null) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
httpDelete.setHeader(entry.getKey(), entry.getValue());
}
}
// 发送请求得到返回数据
httpDelete.setEntity(new StringEntity(param, "UTF-8"));
// 得到响应
response = httpClient.execute(httpDelete);
// 状态码
result.setStatusCode(response.getStatusLine().getStatusCode());
// 响应内容
HttpEntity entity = response.getEntity();
// 响应内容
String responseContent = EntityUtils.toString(entity);
result.setResponseContent(responseContent);
} catch (Exception e) {
throw new CustomException(e);
} finally {
// 关闭流
closeStream(response);
closeStream(httpClient);
}
return result;
} @Override
public ApacheHttpClientResult getForObjectCloud(String uri, Map<String, String> headers) throws CustomException {
// 定义返回
ApacheHttpClientResult result = new ApacheHttpClientResult();
// 定义httpClient和response
CloseableHttpClient httpClient = HttpClientBuilder.create().build();;
CloseableHttpResponse response = null;
try {
// HttpGet
HttpGet httpGet = new HttpGet(uri);
// 设置配置
Builder builder = createBuilder();
RequestConfig config = null;
// 是否开启代理模式访问
if (proxyEnabled) {
HttpHost proxy = new HttpHost(host, port, Proxy.Type.HTTP.toString());
config = builder.setProxy(proxy).build();
} else {
config = builder.build();
}
httpGet.setConfig(config);
// 设置请求头
if (headers != null) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
httpGet.setHeader(entry.getKey(), entry.getValue());
}
}
// 发送请求得到返回数据
response = httpClient.execute(httpGet);
// 状态码
result.setStatusCode(response.getStatusLine().getStatusCode());
// 响应内容
HttpEntity entity = response.getEntity();
// 响应内容
String responseContent = EntityUtils.toString(entity);
result.setResponseContent(responseContent);
} catch (Exception e) {
throw new CustomException(e);
} finally {
// 关闭流
closeStream(response);
closeStream(httpClient);
}
return result;
} @Override
public ApacheHttpClientResult getForObjectCloud(String uri) throws CustomException {
// 定义返回
ApacheHttpClientResult result = new ApacheHttpClientResult();
// 定义httpClient和response
CloseableHttpClient httpClient = HttpClientBuilder.create().build();;
CloseableHttpResponse response = null;
try {
// HttpGet
HttpGet httpGet = new HttpGet(uri);
// 设置配置
Builder builder = createBuilder();
RequestConfig config = null;
// 是否开启代理模式访问
if (proxyEnabled) {
HttpHost proxy = new HttpHost(host, port, Proxy.Type.HTTP.toString());
config = builder.setProxy(proxy).build();
} else {
config = builder.build();
}
httpGet.setConfig(config);
// 发送请求得到返回数据
response = httpClient.execute(httpGet);
// 状态码
result.setStatusCode(response.getStatusLine().getStatusCode());
// 响应内容
HttpEntity entity = response.getEntity();
// 响应内容
String responseContent = EntityUtils.toString(entity, "UTF-8");
result.setResponseContent(responseContent);
} catch (Exception e) {
throw new CustomException(e);
} finally {
// 关闭流
closeStream(response);
closeStream(httpClient);
}
return result;
} @Override
public ApacheHttpClientResult postForJsonNoProxy(String uri, String param) throws CustomException {
// 定义返回
ApacheHttpClientResult result = new ApacheHttpClientResult();
// 定义httpClient和response
CloseableHttpClient httpClient = HttpClientBuilder.create().build();;
CloseableHttpResponse response = null;
try {
// 创建默认的httpClient实例
httpClient = HttpClients.createDefault();
// 定义Post请求
HttpPost httpPost = new HttpPost(uri);
// 设置配置
Builder builder = createBuilder();
RequestConfig config = builder.build();
httpPost.setConfig(config);
// 设置请求头
httpPost.setHeader("Accept", MediaType.APPLICATION_JSON_VALUE);
httpPost.setHeader("Content-Type", MediaType.APPLICATION_JSON_UTF8_VALUE);
// 发送请求得到返回数据
httpPost.setEntity(new StringEntity(param, "UTF-8"));
// 得到响应
response = httpClient.execute(httpPost);
// 状态码
result.setStatusCode(response.getStatusLine().getStatusCode());
// 响应内容
HttpEntity entity = response.getEntity();
// 响应内容
String responseContent = EntityUtils.toString(entity);
result.setResponseContent(responseContent);
} catch (Exception e) {
throw new CustomException(e);
} finally {
// 关闭流
closeStream(response);
closeStream(httpClient);
}
return result;
} @Override
public ApacheHttpClientResult postForJson(String uri, MultipartEntityBuilder fileBuilder) throws CustomException {
// 定义返回
ApacheHttpClientResult result = new ApacheHttpClientResult();
// 定义httpClient和response
CloseableHttpClient httpClient = HttpClientBuilder.create().build();;
CloseableHttpResponse response = null;
try {
// 定义Post请求
HttpPost httpPost = new HttpPost(uri);
// 设置配置
Builder builder = createBuilder();
RequestConfig config = null;
// 是否开启代理模式访问
if (enabled) {
HttpHost proxy = new HttpHost(host, port, Proxy.Type.HTTP.toString());
config = builder.setProxy(proxy).build();
} else {
config = builder.build();
}
httpPost.setConfig(config);
// 设置请求头
//httpPost.setHeader("Content-Type", "multipart/form-data");
// 发送请求得到返回数据
httpPost.setEntity(fileBuilder.build());
// 得到响应
response = httpClient.execute(httpPost);
// 状态码
result.setStatusCode(response.getStatusLine().getStatusCode());
// 响应内容
HttpEntity entity = response.getEntity();
// 响应内容
String responseContent = EntityUtils.toString(entity);
result.setResponseContent(responseContent);
} catch (Exception e) {
throw new CustomException(e);
} finally {
// 关闭流
closeStream(response);
closeStream(httpClient);
}
return result;
} @Override
public ApacheHttpClientResult postForJson(String uri, String param, Map<String, String> headers, Map<String, String> resultHeaders) throws CustomException {
// 定义返回
ApacheHttpClientResult result = new ApacheHttpClientResult();
// 定义httpClient和response
CloseableHttpClient httpClient = HttpClientBuilder.create().build();;
CloseableHttpResponse response = null;
try {
// 定义Post请求
HttpPost httpPost = new HttpPost(uri);
// 设置配置
Builder builder = createBuilder();
RequestConfig config = null;
// 是否开启代理模式访问
if (enabled) {
HttpHost proxy = new HttpHost(host, port, Proxy.Type.HTTP.toString());
config = builder.setProxy(proxy).build();
} else {
config = builder.build();
}
httpPost.setConfig(config);
// 设置请求头
httpPost.setHeader("Accept", MediaType.APPLICATION_JSON_VALUE);
httpPost.setHeader("Content-Type", MediaType.APPLICATION_JSON_UTF8_VALUE);
if (headers != null) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
httpPost.setHeader(entry.getKey(), entry.getValue());
}
}
// 发送请求得到返回数据
httpPost.setEntity(new StringEntity(param, "UTF-8"));
// 得到响应
response = httpClient.execute(httpPost);
// 状态码
result.setStatusCode(response.getStatusLine().getStatusCode());
// 响应内容
HttpEntity entity = response.getEntity();
Header header = response.getFirstHeader("Location");
if (header != null && StringUtils.isNotBlank(header.getValue())) {
String location = header.getValue();
String maCertificateId = location.substring(header.getValue().lastIndexOf('/') + 1, location.length());
resultHeaders.put("Location", maCertificateId);
}
// 响应内容
String responseContent = EntityUtils.toString(entity);
result.setResponseContent(responseContent);
} catch (Exception e) {
throw new CustomException(e);
} finally {
// 关闭流
closeStream(response);
closeStream(httpClient);
}
return result;
} @Override
public ApacheHttpClientResult putForJson(String uri, String param, Map<String, String> headers,
Map<String, String> resultHeaders) throws CustomException {
// 定义返回
ApacheHttpClientResult result = new ApacheHttpClientResult();
// 定义httpClient和response
CloseableHttpClient httpClient = HttpClientBuilder.create().build();;
CloseableHttpResponse response = null;
try {
// HttpPut
HttpPut httpPut = new HttpPut(uri);
// 设置配置
Builder builder = createBuilder();
RequestConfig config = null;
// 是否开启代理模式访问
if (enabled) {
HttpHost proxy = new HttpHost(host, port, Proxy.Type.HTTP.toString());
config = builder.setProxy(proxy).build();
} else {
config = builder.build();
}
httpPut.setConfig(config);
// 设置请求头
httpPut.setHeader("Accept", MediaType.APPLICATION_JSON_VALUE);
httpPut.setHeader("Content-Type", MediaType.APPLICATION_JSON_UTF8_VALUE);
if (headers != null) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
httpPut.setHeader(entry.getKey(), entry.getValue());
}
}
// 发送请求得到返回数据
httpPut.setEntity(new StringEntity(param, "UTF-8"));
// 得到响应
response = httpClient.execute(httpPut);
// 状态码
result.setStatusCode(response.getStatusLine().getStatusCode());
// 响应内容
HttpEntity entity = response.getEntity();
// 响应内容
String responseContent = EntityUtils.toString(entity);
result.setResponseContent(responseContent);
Header header = response.getFirstHeader("Location");
if (header != null && StringUtils.isNotBlank(header.getValue())) {
String location = header.getValue();
String maCertificateId = location.substring(header.getValue().lastIndexOf('/') + 1, location.length());
resultHeaders.put("Location", maCertificateId);
}
LOGGER.info(uri + "&&&&&" + response.toString() + "&&&&&" + param);
} catch (Exception e) {
throw new CustomException(e);
} finally {
// 关闭流
closeStream(response);
closeStream(httpClient);
}
return result;
} public static void closeStream(Closeable c) {
// 流不为空
if (c != null) {
try {
// 流关闭
c.close();
} catch (IOException ex) {
LOGGER.error("closeStream failed", ex);
}
}
}
}

5.依赖的类:

@NotThreadSafe
public class HttpDeleteWithBody extends HttpEntityEnclosingRequestBase { private static final String METHOD_NAME = "DELETE"; /**
* 获取方法(必须重载)
*
* @return
*/
@Override
public String getMethod() {
return METHOD_NAME;
} public HttpDeleteWithBody(final String uri) {
super();
setURI(URI.create(uri));
} public HttpDeleteWithBody(final URI uri) {
super();
setURI(uri);
} public HttpDeleteWithBody() {
super();
}
}
public class CustomException extends Exception {

    private static final long serialVersionUID = 8984728932846627819L;

    public CustomException() {
super();
} public CustomException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
} /** public CustomException(String message, Throwable cause) {
super(message, cause);
} public CustomException(String message) {
super(message);
} public CustomException(Throwable cause) {
super(cause);
} }

http各类型请求方法工具总结的更多相关文章

  1. Http协议请求方法及body类型(思路比较清晰的)

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/u010244522/article/de ...

  2. HTTP请求响应报文&&相关状态码&&GET_POST请求方法 总结

    HTTP请求报文: 一个HTTP请求报文由四个部分组成:请求行.请求头部.空行.请求数据 1.请求行   请求行由请求方法字段.URL字段和HTTP协议版本字段3个字段组成,它们用空格分隔.比如 GE ...

  3. [爬虫进阶]使用Jsoup取代你的一切网络请求方法(java,post,get,代理IP)

    [爬虫进阶]使用Jsoup取代你的一切网络请求方法(java,post,get,代理IP) 原文链接:https://www.cnblogs.com/blog5277/p/9334560.html 原 ...

  4. HTTP/1.1标准请求方法和状态码

    HTTP/1.1标准自从1999年制定以来至今仍然是一个应用广泛并且通行的标准 相关文档 RFC2616:Hypertext Transfer Protocol -- HTTP/1.1 在RFC658 ...

  5. 简述HTTP报文请求方法和状态响应码

    1. Method 请求方法,表明客户端希望服务器对资源执行的动作: 1.1 GET 向服务器请求资源. 1.2 HEAD 和GET方法的行为类似,但服务器在响应中只返回首部,不会返回实体的主体部分. ...

  6. HTTP请求方法及响应码详解(http get post head)

      HTTP是Web协议集中的重要协议,它是从客户机/服务器模型发展起来的.客户机/服务器是运行一对相互通信的程序,客户与服务器连接时,首先,向服务 器提出请求,服务器根据客户的请求,完成处理并给出响 ...

  7. HTTP请求方法详解

    HTTP请求方法详解 请求方法:指定了客户端想对指定的资源/服务器作何种操作 下面我们介绍HTTP/1.1中可用的请求方法: [GET:获取资源]     GET方法用来请求已被URI识别的资源.指定 ...

  8. iOS开发 GET、POST请求方法(NSURLConnection篇)

    Web Service使用的主要协议是HTTP协议,即超文本传输协议. HTTP/1.1协议共定义了8种请求方法(OPTIONS.HEAD.GET.POST.PUT.DELETE.TRACE.CONN ...

  9. HTTP协议中POST、GET、HEAD、PUT等请求方法以及一些常见错误

    (来源:http://www.tuicool.com/articles/Ermmmyn) HTTP请求方法: 常用方法: Get\Post\Head (1)Get方法. 取回请求URL标志的任何信息, ...

随机推荐

  1. E2E测试工具之--01 Cypress 上手使用

    The web has evolved. Finally, testing has too. 1. 简介 cypress 最近很火的e2e(即end to end(端到端))测试框架,它基于node ...

  2. css浮动float详解

    https://www.cnblogs.com/iyangyuan/archive/2013/03/27/2983813.html

  3. mysql修改表结构,添加double类型新列

    ALTER TABLE t_cas_construction_statistics ADD COLUMN resource_one_online_count DOUBLE(128,0) COMMENT ...

  4. k8s之pod与Pod控制器

    k8s中最为重要的基础资源,pod,pod controller,service pod controller类型有多种需要向控制器赋值之后使用: kubectl命令使用 kubectk get no ...

  5. cmake 的简单使用示例

    https://www.zybuluo.com/khan-lau/note/254724 CMake 用法导览 Preface : 本文是CMake官方文档CMake Tutorial (http:/ ...

  6. MySQL/MariaDB数据库的复制监控和维护

      MySQL/MariaDB数据库的复制监控和维护 作者:尹正杰  版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.清理日志 1>.删除指定日志文件名称之前的日志(也可用基于时间) M ...

  7. 如何在Jenkins中使用日期参数(变量)

    一.首先需要安装插件:Date Parameter Plugin 二.安装完成后,在项目中添加参数,我这里只有日期,时间的话没试过,应该也可以把 三.用${date}调用参数即可 最后邮件的附件正常~

  8. 移动架构师第一站UML建模

    回想一下自己的Android生涯已经经历过N多个年头了,基本都是在编写业务代码,都知道35岁程序员是一个坎,当然如果有能力能做到Android架构师的职位其生命周期也会较长,毕境不是人人都能轻易做到这 ...

  9. 第七篇:ORM框架SQLAlchemy

    阅读目录 一 介绍 二 创建表 三 增删改查 四 其他查询相关 五 正查.反查 一 介绍 SQLAlchemy是Python编程语言下的一款ORM框架,该框架建立在数据库API之上,使用关系对象映射进 ...

  10. spark-scala开发的第一个程序WordCount

    package ***** import org.apache.spark.{SparkConf, SparkContext} object WordCount { def main(args: Ar ...