Http客户端工具
| 文章目录 |
|---|
| Maven打包Jar + Http向Url发送Get请求_lijiamin-的CSDN博客 |
前段时间,我写了一个非常简陋的Get请求的代码用于刷CSDN的访客量,但是那个玩意并不能用于实际工作中,因为它里面并没有包含什么其他的参数,只是简简单单的发起请求,但是在这几天的工作中,我有幸接触到了这篇代码,现将它刨析一番并分享给大家
用途及分析
顾名思义,是一个HTTP客户端工具,能通过代码方式发起get、post、put、delete请求,里面有如下几个方法
- 连接池初始化
- 连接保活(长连接)
- SSL绕过验证
- 请求创建
- 执行请求(GET\POST\PUT\DELETE)
关于请求,我们需要同时考虑到HTTP/HTTPS的情况,HTTPS采用了SSL的安全加密

初始化的客户端,里面的传入实例defaultStrategy是HTTP长连接的配置

接下来开始创建请求

请求的执行我不太想写了,我去整其他东西了,完整的代码在下面
完整的代码段
使用方式
HttpClientPoolUtil.post(完整的请求地址, JSON字符串的请求数据, 请求头);
实际案例代码
public static void main(String[] args) throws Exception {
String file = "D:\\Weather-local.txt";
FileWriter fw = new FileWriter(file, true);
while (true) {
// 请求
Map map = new HashMap<String, Object>();
map.put("accept", "*/*");
map.put("connection", "Keep-Alive");
map.put("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
map.put("Content-Type", "application/json;charset=utf-8");
String strRes = HttpClientPoolUtil.get("http://t.weather.itboy.net/api/weather/city/101280601", map);
// 写处理
fw.append("当前时间:" + new Date() + "_____________" + strRes);
fw.append("\n");
fw.append("\n");
fw.flush();
System.out.println(new Date() + "----ok");
Thread.sleep(900000);
}
}
原始代码
import org.apache.http.HeaderElement;
import org.apache.http.HeaderElementIterator;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.*;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.ConnectionKeepAliveStrategy;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicHeaderElementIterator;
import org.apache.http.protocol.HTTP;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.nio.charset.StandardCharsets;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.Map;
import java.util.Map.Entry;
public class HttpClientPoolUtil {
/**
* 也实现Closeable的HttpClient基本实现
*/
public static CloseableHttpClient httpClient = null;
/**
* 初始化连接池
*
* @throws NoSuchAlgorithmException
* @throws KeyManagementException
*/
public static synchronized void initPools() throws KeyManagementException, NoSuchAlgorithmException {
if (httpClient == null) {
// 采用绕过验证的方式处理https请求
SSLContext sslcontext = createIgnoreVerifySSL();
// 设置协议http和https对应的处理socket链接工厂的对象
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.INSTANCE)
.register("https", new SSLConnectionSocketFactory(sslcontext, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER))
.build();
// ClientConnectionPoolManager维护一个HttpClientConnection池
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
cm.setDefaultMaxPerRoute(20);
cm.setMaxTotal(500);
httpClient = HttpClients.custom().setKeepAliveStrategy(defaultStrategy).setConnectionManager(cm).build();
}
}
/**
* Http connection keepAlive 设置
*/
public static ConnectionKeepAliveStrategy defaultStrategy = new ConnectionKeepAliveStrategy() {
@Override
public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
HeaderElementIterator it = new BasicHeaderElementIterator(response.headerIterator(HTTP.CONN_KEEP_ALIVE));
int keepTime = 30;
while (it.hasNext()) {
HeaderElement he = it.nextElement();
String param = he.getName();
String value = he.getValue();
if (value != null && param.equalsIgnoreCase("timeout")) {
try {
return Long.parseLong(value) * 1000;
} catch (Exception e) {
e.printStackTrace();
}
}
}
return keepTime * 1000;
}
};
/**
* 绕过验证
*
* @return
* @throws NoSuchAlgorithmException
* @throws KeyManagementException
*/
public static SSLContext createIgnoreVerifySSL() throws NoSuchAlgorithmException, KeyManagementException {
SSLContext sc = SSLContext.getInstance("SSLv3");
// 实现一个X509TrustManager接口,用于绕过验证,不用修改里面的方法
X509TrustManager trustManager = new X509TrustManager() {
@Override
public void checkClientTrusted(
java.security.cert.X509Certificate[] paramArrayOfX509Certificate,
String paramString) {
}
@Override
public void checkServerTrusted(
java.security.cert.X509Certificate[] paramArrayOfX509Certificate,
String paramString) {
}
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
};
sc.init(null, new TrustManager[]{trustManager}, null);
return sc;
}
/**
* 创建请求
*
* @param url 请求url
* @param methodName 请求的方法类型
* @param headMap 请求头
* @return
* @throws NoSuchAlgorithmException
* @throws KeyManagementException
*/
public static HttpRequestBase getRequest(String url, String methodName, Map<String, String> headMap)
throws KeyManagementException, NoSuchAlgorithmException {
if (httpClient == null) {
initPools();
}
HttpRequestBase method = null;
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(30 * 1000).setConnectTimeout(30 * 1000)
.setConnectionRequestTimeout(30 * 1000).setExpectContinueEnabled(false).build();
if (HttpPut.METHOD_NAME.equalsIgnoreCase(methodName)) {
method = new HttpPut(url);
} else if (HttpPost.METHOD_NAME.equalsIgnoreCase(methodName)) {
method = new HttpPost(url);
} else if (HttpGet.METHOD_NAME.equalsIgnoreCase(methodName)) {
method = new HttpGet(url);
} else if (HttpDelete.METHOD_NAME.equalsIgnoreCase(methodName)) {
method = new HttpDelete(url);
} else {
method = new HttpPost(url);
}
if (!headMap.isEmpty()) {
for (Entry<String, String> value : headMap.entrySet()) {
method.addHeader(value.getKey(), value.getValue());
}
}
method.setConfig(requestConfig);
return method;
}
/**
* 执行GET 请求
*
* @param url
* @return
*/
public static String get(String url, Map<String, String> headMap) throws Exception {
HttpEntity httpEntity = null;
HttpRequestBase method = null;
String responseBody = "";
try {
if (httpClient == null) {
initPools();
}
method = getRequest(url, HttpGet.METHOD_NAME, headMap);
HttpContext context = HttpClientContext.create();
CloseableHttpResponse httpResponse = httpClient.execute(method, context);
httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
responseBody = EntityUtils.toString(httpEntity, "UTF-8");
}
} catch (Exception e) {
if (method != null) {
method.abort();
}
throw e;
} finally {
if (httpEntity != null) {
try {
EntityUtils.consumeQuietly(httpEntity);
} catch (Exception e) {
throw e;
}
}
}
return responseBody;
}
/**
* 执行http post请求 默认采用Content-Type:application/json,Accept:application/json
*
* @param url 请求地址
* @param data 请求数据
* @param data 请求头
* @return
*/
public static String post(String url, String data, Map<String, String> headMap) throws Exception {
HttpEntity httpEntity = null;
HttpEntityEnclosingRequestBase method = null;
String responseBody = "";
try {
if (httpClient == null) {
initPools();
}
method = (HttpEntityEnclosingRequestBase) getRequest(url, HttpPost.METHOD_NAME, headMap);
method.setEntity(new StringEntity(data, StandardCharsets.UTF_8));
HttpContext context = HttpClientContext.create();
CloseableHttpResponse httpResponse = httpClient.execute(method, context);
httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
responseBody = EntityUtils.toString(httpEntity, "UTF-8");
}
} catch (Exception e) {
if (method != null) {
method.abort();
}
throw e;
} finally {
if (httpEntity != null) {
try {
EntityUtils.consumeQuietly(httpEntity);
} catch (Exception e) {
throw e;
}
}
}
return responseBody;
}
/**
* 执行http put请求 默认采用Content-Type:application/json,Accept:application/json
*
* @param url 请求地址
* @param data 请求数据
* @param data 请求头
* @return
*/
public static String put(String url, String data, Map<String, String> headMap) throws Exception {
HttpEntity httpEntity = null;
HttpEntityEnclosingRequestBase method = null;
String responseBody = "";
try {
if (httpClient == null) {
initPools();
}
method = (HttpEntityEnclosingRequestBase) getRequest(url, HttpPut.METHOD_NAME, headMap);
method.setEntity(new StringEntity(data, StandardCharsets.UTF_8));
HttpContext context = HttpClientContext.create();
CloseableHttpResponse httpResponse = httpClient.execute(method, context);
httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
responseBody = EntityUtils.toString(httpEntity, "UTF-8");
}
} catch (Exception e) {
if (method != null) {
method.abort();
}
throw e;
} finally {
if (httpEntity != null) {
try {
EntityUtils.consumeQuietly(httpEntity);
} catch (Exception e) {
throw e;
}
}
}
return responseBody;
}
/**
* 执行DELETE 请求
*
* @param url
* @return
*/
public static String delete(String url, Map<String, String> headMap) throws Exception {
HttpEntity httpEntity = null;
HttpRequestBase method = null;
String responseBody = "";
try {
if (httpClient == null) {
initPools();
}
method = getRequest(url, HttpDelete.METHOD_NAME, headMap);
HttpContext context = HttpClientContext.create();
CloseableHttpResponse httpResponse = httpClient.execute(method, context);
httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
responseBody = EntityUtils.toString(httpEntity, "UTF-8");
}
} catch (Exception e) {
if (method != null) {
method.abort();
}
throw e;
} finally {
if (httpEntity != null) {
try {
EntityUtils.consumeQuietly(httpEntity);
} catch (Exception e) {
throw e;
}
}
}
return responseBody;
}
}
结束
版权声明:本文为weixin_48518621原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。