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
原创粉丝点击