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

  在项目中会用到各种类型的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. jetbreains的crack方法

    https://zhile.io/2018/08/20/jetbrains-license-server-crack.html

  2. 快捷定位目录 z武器

    z的源码在这里:https://github.com/rupa/z/blob/master/z.sh 1.把源码复制到你的用户目录下的z.sh文件, 2.然后用vim打开.bashrc这个目录,在最后 ...

  3. SQL进阶系列之10HAVING子句又回来了

    写在前面 HAVING子句的处理对象是集合而不是记录 各队,全队点名 --各队,全体点名! CREATE TABLE Teams (member CHAR(12) NOT NULL PRIMARY K ...

  4. Relief 过滤式特征选择

    给定训练集{(x1,y1),(x2,y2).....(xm,ym)} ,对每个示例xi,Relief在xi的同类样本中寻找其最近邻xi,nh(猜中近邻),再从xi的异类样本中寻找其最近邻xi,nm(猜 ...

  5. anyproxy学习2-rule模块实现接口mock功能

    前言 AnyProxy不仅仅可以抓包,还可以拦截请求并修改服务端响应,实现接口mock功能. 面试时候经常会问到第三方支付如何测试这种,如果对接的第三方没提供测试环境,那么就需要搭建一个mock服务器 ...

  6. 51nod 2502 最多分成多少块

    小b有个长度为n的数组a,她想将这个数组排序. 然而小b很懒,她觉得对整个数组排序太累了,因此她请你将a分成一些块,使得她只需要对每一块分别排序,就能将整个数组排序. 请问你最多能把a分成多少块. 保 ...

  7. libpng 漏洞分析

    相关资源 PNG文件格式文档 http://www.libpng.org/pub/png/spec/1.2/PNG-Chunks.html https://www.myway5.com/index.p ...

  8. Docker 部署 vue 项目

    Docker 部署 vue 项目 Docker 作为轻量级虚拟化技术,拥有持续集成.版本控制.可移植性.隔离性和安全性等优势.本文使用Docker来部署一个vue的前端应用,并尽可能详尽的介绍了实现思 ...

  9. 通过vue-cli搭建vue项目

    一.搭建环境 安装node.js 从node.js官网下载并安装node,安装过程很简单,一路“下一步”就可以了.安装完成之后,打开命令行工具(win+r,然后输入cmd),输入 node -v,如下 ...

  10. django-购物车添加

    商品详情页detail.html添加加入购物车按钮 <a href="javascript:;" sku_id="{{ sku.id }}" class= ...