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

  在项目中会用到各种类型的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. 使用Docker搭建Elasticsearch集群环境

    本篇文章首发于头条号单机如何搭建Elasticsearch集群?使用容器技术快速构建集群环境,欢迎关注头条号和微信公众号"大数据技术和人工智能"(微信搜索bigdata_ai_te ...

  2. python之路第一天

    2018-07-11星期三 创建自己的博客(博客园): 登陆 我的博客 随笔:所有人在博客中都能看见的文章 文章:别人看不见,只能URL访问--我把网页地址发给你,你才能看到 日志:别人看不到,URL ...

  3. USB之设备插入波形变化2

    =============  本系列参考  ============= <圈圈教你玩USB>.<Linux那些事儿之我是USB> 协议文档:https://www.usb.or ...

  4. pyecharts绘制geo地图

    pyecharts是一种非常强大的绘图python库,绘制的图形非常好看,并且有代表性,不仅仅是地图,还可以绘制条形图.饼图.词云图等等. # 安装方法 pip install pyecharts # ...

  5. Pthon魔术方法(Magic Methods)-bool

    Pthon魔术方法(Magic Methods)-bool 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.bool方法 __bool__: 内建函数bool(),或者对象放在逻 ...

  6. Tomcat--安装部署

    Tomcat安装部署 Tomcat简介 官网:http://tomcat.apache.org/ Tomcat服务器是一个免费的开源代码的Web应用服务器,属于轻量级应用服务器,在中小型系统和并发访问 ...

  7. 常用conda命令【转载】

    转载地址:https://haoyu.love/blog900.html 一直在用 Conda,很多东西记不住,每次都要查 Doc.那好,就写在这里做个备忘好了. 在 bash 里面自动加载 cond ...

  8. CentOS7:sorry,that didn't work.please try again!

    参考以下解决方案,重点是vi etc/selinux/config 把 enforcing 改为 disable 应用场景 linux管理员忘记root密码,需要进行找回操作.注意事项:本文基于cen ...

  9. C#使用ODP.NET(Oracle.ManagedDataAccess.dll)操作Oracle数据库

    在刚接触C#的时候由于公司使用的就是Oracle数据库,那么C#怎么连接Oracle数据库就成了首要去掌握的知识点了.在那时没有ODP.NET,但visual studio却对Oralce数据库的调用 ...

  10. 《逆袭团队》第八次团队作业:Alpha冲刺

    项目 内容 软件工程 任课教师博客主页链接 作业链接地址 团队作业8:Alpha冲刺 团队名称 逆袭团队 具体目标 完成最后冲刺阶段的5次博客 一.团队项目github仓库地址:Github 二.Sc ...