apache httpcompontens之HttpAsyncClient使用
来源:互联网 发布:金中投证券软件下载 编辑:程序博客网 时间:2024/05/18 15:28
闲来无事,研究了会HttpAsyncClient,写了一个工具类,替代现有的http工具类
先是maven依赖
<dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpasyncclient</artifactId><version>4.1.1</version></dependency><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpasyncclient-cache</artifactId><version>4.1.1</version></dependency>
再是工具类代码
import java.util.Map;import java.util.Set;import java.util.concurrent.Future;import java.util.concurrent.TimeUnit;import org.apache.http.Consts;import org.apache.http.HttpResponse;import org.apache.http.client.config.RequestConfig;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpUriRequest;import org.apache.http.client.methods.RequestBuilder;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;import org.apache.http.impl.nio.client.HttpAsyncClients;import org.apache.http.util.EntityUtils;/** * http处理类 * 使用apache的HttpAsyncClient * @author zhangle */public class HttpAsyncUtil {private static final String DEFAULT_CHARSET = "UTF-8"; public static String get(String url) throws Exception { return asyncRequest("get",url,null,null); } public static String get(String url, Map<String, String> params) throws Exception { return asyncRequest("get",url,params,null); } public static String post(String url) throws Exception { return asyncRequest("post",url,null,null); } public static String post(String url, Map<String, String> params) throws Exception { return asyncRequest("post",url,params,null); } public static String asyncRequest(String method,String url, Map<String, String> params, Map<String, String> headers) throws Exception { RequestConfig requestConfig = RequestConfig.custom() .setSocketTimeout(6000) .setConnectTimeout(6000) .setConnectionRequestTimeout(6000) .build();CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom().setDefaultRequestConfig(requestConfig).build();try { httpclient.start(); RequestBuilder builder=RequestBuilder.create(method.toUpperCase()).setUri(url); builder.setCharset(Consts.UTF_8); builder.setConfig(requestConfig); if (params != null && !params.isEmpty()) { Set<String> keys = params.keySet(); for (String key : keys) { builder.addParameter(key, params.get(key)); }}if (headers != null && !headers.isEmpty()) { Set<String> keys = headers.keySet(); for (String key : keys) { builder.addHeader(key, headers.get(key)); }}HttpUriRequest request = builder.build(); Future<HttpResponse> future = httpclient.execute(request, null); HttpResponse response = future.get(6000,TimeUnit.MILLISECONDS); return EntityUtils.toString(response.getEntity(), DEFAULT_CHARSET);} finally { httpclient.close();} } public static String request(String method,String url, Map<String, String> params, Map<String, String> headers) throws Exception { RequestConfig requestConfig = RequestConfig.custom() .setSocketTimeout(6000) .setConnectTimeout(6000) .setConnectionRequestTimeout(6000) .build();CloseableHttpClient httpclient = HttpClients.custom().setDefaultRequestConfig(requestConfig).build();try { RequestBuilder builder=RequestBuilder.create(method.toUpperCase()).setUri(url); builder.setCharset(Consts.UTF_8); builder.setConfig(requestConfig); if (params != null && !params.isEmpty()) { Set<String> keys = params.keySet(); for (String key : keys) { builder.addParameter(key, params.get(key)); }}if (headers != null && !headers.isEmpty()) { Set<String> keys = headers.keySet(); for (String key : keys) { builder.addHeader(key, headers.get(key)); }}HttpUriRequest request = builder.build();CloseableHttpResponse response = httpclient.execute(request); return EntityUtils.toString(response.getEntity(), DEFAULT_CHARSET);} finally { httpclient.close();} }}
然后是使用工具类
import org.junit.Test;import com.lige.sms.util.HttpAsyncUtil;public class HttpTest {@Testpublic void test() throws Exception {String content=HttpAsyncUtil.get("http://www.apache.org/");System.out.println(content);}}
0 0
- apache httpcompontens之HttpAsyncClient使用
- HttpAsyncClient的连接池使用
- 异步httpclient---HttpAsyncClient的使用
- HttpAsyncClient之模拟post请求示例
- 异步HttpAsyncClient框架的学习和使用
- 使用异步HTTP提升客户端性能(HttpAsyncClient)
- 使用异步HTTP提升客户端性能(HttpAsyncClient)
- HttpAsyncClient使用中碰到的问题
- 异步httpclient(httpasyncclient)的使用与总结
- 轻松把玩HttpAsyncClient之模拟post请求示例
- 轻松把玩HttpAsyncClient之模拟post请求示例
- 轻松把玩HttpAsyncClient之模拟post请求示例
- 轻松把玩HttpAsyncClient之模拟post请求示例
- HttpAsyncClient (异步http 请求)的连接池使用
- HttpAsyncClient 介绍
- APACHE优化之apache的内存使用
- APACHE优化之apache的内存使用
- Apache之DBUtils的使用
- win32汇编 屏幕截图保存BMP 学习笔记<第四篇>之生成BMP文件
- Java并发编程:深入剖析ThreadLocal
- <<web>>漂亮的相册(一)
- 解决:[Ljava.lang.Object; cannot be cast to com.sinone.domain.StorageCount
- Laravel课程表的数据库设计与实现
- apache httpcompontens之HttpAsyncClient使用
- mac 开发环境配置之homebrew
- android项目打包成jar
- HorizontalListView 横向listview
- Delete log files longer than 45 days
- 实用微博链接
- 15.4.1 杠杆利用类型参数推断
- 1153 - Paquet plus grand que 'max_allowed_packet' (navicat)
- oracle 触发器的实例