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

  在项目中会用到各种类型的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. php 函数阶乘理解

    <?php //函数阶乘 函数调用自身,函数在执行的时候每次都会开辟一个空间,如 /** * $a =3的话,首先判断 3>1 为真 $r=3*demo(3-1) 开辟一个空间调用自身. ...

  2. 10 分钟上手 Vue 组件 Vue-Draggable

    Vue 综合了 Angualr 和 React 的优点,因其易上手,轻量级,受到了广泛应用.成为了是时下火热的前端框架,吸引着越来越多的前端开发者! 本文将通过一个最简单的拖拽例子带领大家快速上手 V ...

  3. 发布WS接口与实现WS接口[小列子]

    webservice简介:Web Service技术, 能使得运行在不同机器上的不同应用无须借助附加的.专门的第三方软件或硬件, 就可相互交换数据或集成.依据Web Service规范实施的应用之间, ...

  4. javascript原生图片懒加载

    一,原生javascript图片懒加载 1. 使用方法,例如 // 要绑定的图片地址 <img data-src={url} alt=" "> 2. 在页面中引入下列原 ...

  5. jquery 表单对象属性筛选选择器

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-type" content ...

  6. HDU-4544 湫湫系列故事——消灭兔子 (贪心+优先队列)

    题目思路 将兔子的血量从大到小排列,将箭的属性写在类中(结构体也成),排序按照伤害从大到小排列,若有相等的则按价格从小到大排. 代码 #include<bits/stdc++.h> usi ...

  7. python3爬虫--反爬虫应对机制

    python3爬虫--反爬虫应对机制 内容来源于: Python3网络爬虫开发实战: 网络爬虫教程(python2): 前言: 反爬虫更多是一种攻防战,针对网站的反爬虫处理来采取对应的应对机制,一般需 ...

  8. oracle 年龄分档,不用case when 的方法

    一般我们出分档数据都是case when ,但是如果是对年龄等一些字段进行细分,比如五岁一档,我们如果用case when就会特别麻烦,写的特别多,这里我介绍一种简单的方法,对细分的字段进行处理: 建 ...

  9. Linux——CentOS7没有第二张网卡的配置信息

    前言 为了一个实验做测试,在VMware中配置了环境,但是配置了双网卡后发现第二张网卡没有配置文件. 都是些基本命令就不写了,图里也有. 系统 : CentOS7.6 步骤 查看网卡信息 使用ip a ...

  10. Scanner的常用用法

    通过new Scanner(System.in)创建一个Scanner,控制台会一直等待输入,直到敲回车键结束,把所输入的内容传给Scanner. s.useDelimiter(" |,|\ ...