http各类型请求方法工具总结
本文为博主原创,未经允许不得转载:
在项目中会用到各种类型的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各类型请求方法工具总结的更多相关文章
- Http协议请求方法及body类型(思路比较清晰的)
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/u010244522/article/de ...
- HTTP请求响应报文&&相关状态码&&GET_POST请求方法 总结
HTTP请求报文: 一个HTTP请求报文由四个部分组成:请求行.请求头部.空行.请求数据 1.请求行 请求行由请求方法字段.URL字段和HTTP协议版本字段3个字段组成,它们用空格分隔.比如 GE ...
- [爬虫进阶]使用Jsoup取代你的一切网络请求方法(java,post,get,代理IP)
[爬虫进阶]使用Jsoup取代你的一切网络请求方法(java,post,get,代理IP) 原文链接:https://www.cnblogs.com/blog5277/p/9334560.html 原 ...
- HTTP/1.1标准请求方法和状态码
HTTP/1.1标准自从1999年制定以来至今仍然是一个应用广泛并且通行的标准 相关文档 RFC2616:Hypertext Transfer Protocol -- HTTP/1.1 在RFC658 ...
- 简述HTTP报文请求方法和状态响应码
1. Method 请求方法,表明客户端希望服务器对资源执行的动作: 1.1 GET 向服务器请求资源. 1.2 HEAD 和GET方法的行为类似,但服务器在响应中只返回首部,不会返回实体的主体部分. ...
- HTTP请求方法及响应码详解(http get post head)
HTTP是Web协议集中的重要协议,它是从客户机/服务器模型发展起来的.客户机/服务器是运行一对相互通信的程序,客户与服务器连接时,首先,向服务 器提出请求,服务器根据客户的请求,完成处理并给出响 ...
- HTTP请求方法详解
HTTP请求方法详解 请求方法:指定了客户端想对指定的资源/服务器作何种操作 下面我们介绍HTTP/1.1中可用的请求方法: [GET:获取资源] GET方法用来请求已被URI识别的资源.指定 ...
- iOS开发 GET、POST请求方法(NSURLConnection篇)
Web Service使用的主要协议是HTTP协议,即超文本传输协议. HTTP/1.1协议共定义了8种请求方法(OPTIONS.HEAD.GET.POST.PUT.DELETE.TRACE.CONN ...
- HTTP协议中POST、GET、HEAD、PUT等请求方法以及一些常见错误
(来源:http://www.tuicool.com/articles/Ermmmyn) HTTP请求方法: 常用方法: Get\Post\Head (1)Get方法. 取回请求URL标志的任何信息, ...
随机推荐
- MyBatis 中如何调用 Java 的 enum (枚举) 字段
事先作成一 enum,如下: public enum CityCode { ALL("000"), BEIJING("010"), SHANGHAI(" ...
- MySQL Processlist--查看会话执行过的SQL情况
对于MySQL 5.7版本,可以使用sys.session视图来查看会话最后一次执行的SQL: SELECT * FROM sys.session WHERE CONN_ID = \G 其中sys.s ...
- 大数据集群环境 zookeeper集群环境安装
大数据集群环境 zookeeper集群环境准备 zookeeper集群安装脚本,如果安装需要保持zookeeper保持相同目录,并且有可执行权限,需要准备如下 编写脚本: vi zkInstall.s ...
- Linux操作系统的进程管理和作业管理
Linux操作系统的进程管理和信号 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.lsof命令详解 1>.lsof概述 list open files查看当前系统文件的工 ...
- Django设置应用名与模型名为中文
修改polls包里面的apps.py: from django.apps import AppConfig class PollsConfig(AppConfig): name = 'polls' v ...
- zookeeper题目
1. ZooKeeper是什么?2. ZooKeeper提供了什么?3. Zookeeper文件系统4. ZAB协议?5. 四种类型的数据节点 Znode6. Zookeeper Watcher 机制 ...
- 51nod 1115 最大M子段和 V3
环形最大M子段和,N个整数组成的序列排成一个环,a[1],a[2],a[3],…,a[n](a[n-1], a[n], a[1]也可以算作1段),将这N个数划分为互不相交的M个子段,并且这M个子段的和 ...
- .netcore发布时指定服务器的系统类型
asp.net core 开发完成后发布,在IIS上面访问,直接报错 系统是windows2008 Application startup exception: System.DllNotFound ...
- JAVA 启动服务命令
java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=7301,suspend=n -Xms2048m -Xmx4096m -XX:+ ...
- LeetCode 1046. Last Stone Weight
原题链接在这里:https://leetcode.com/problems/last-stone-weight/ 题目: We have a collection of rocks, each roc ...