文档章节

httpclient工具类

WallacePang
 WallacePang
发布于 2016/08/05 12:38
字数 641
阅读 24
收藏 0

工作中整理的一个基于httpclient4.3.4 的工具类

import com.google.common.base.Optional;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HeaderElement;
import org.apache.http.HeaderElementIterator;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
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.utils.URIBuilder;
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.LayeredConnectionSocketFactory;
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.HttpClientBuilder;
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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

public class HttpClient4Util {
    private final static Logger logger = LoggerFactory
            .getLogger(HttpClient4Util.class);
    private CloseableHttpClient httpclient;
    private RequestConfig requestConfig;
    private PoolingHttpClientConnectionManager connManager;
    private static HttpClient4Util httpClient4UtilNew;
    private volatile boolean shutdown;

    public synchronized static HttpClient4Util getInstance() throws Exception {
        if (httpClient4UtilNew == null) {
            httpClient4UtilNew = new HttpClient4Util();
            httpClient4UtilNew.init();
        }
        return httpClient4UtilNew;
    }

    private HttpClient4Util() {

    }

    /**
     * 构造函数
     */
    public void init() throws NoSuchAlgorithmException, KeyManagementException {
        SSLContext sslCtx = SSLContext.getInstance("TLS");
        X509TrustManager trustManager = new X509TrustManager() {
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }

            public void checkClientTrusted(X509Certificate[] arg0, String arg1)
                    throws CertificateException {
            }

            public void checkServerTrusted(X509Certificate[] arg0, String arg1)
                    throws CertificateException {
            }
        };
        sslCtx.init(null, new TrustManager[]{trustManager}, null);
        LayeredConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(
                sslCtx, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        RegistryBuilder<ConnectionSocketFactory> registryBuilder = RegistryBuilder
                .<ConnectionSocketFactory>create();
        ConnectionSocketFactory plainSocketFactory = new PlainConnectionSocketFactory();
        registryBuilder.register("http", plainSocketFactory);
        registryBuilder.register("https", sslSocketFactory);

        Registry<ConnectionSocketFactory> registry = registryBuilder.build();
        // 设置连接管理器
        connManager = new PoolingHttpClientConnectionManager(registry);
        // 设置最大连接数
        connManager.setMaxTotal(200);
        // 设置每个路由基础的连接
        connManager.setDefaultMaxPerRoute(20);

        // 连接保持活跃策略
        ConnectionKeepAliveStrategy myStrategy = new ConnectionKeepAliveStrategy() {
            @Override
            public long getKeepAliveDuration(HttpResponse response,
                                             HttpContext context) {
                // 获取'keep-alive'HTTP报文头
                HeaderElementIterator it = new BasicHeaderElementIterator(
                        response.headerIterator(HTTP.CONN_KEEP_ALIVE));
                while (it.hasNext()) {
                    HeaderElement he = it.nextElement();
                    String param = he.getName();
                    String value = he.getValue();
                    if (null != value && "timeout".equalsIgnoreCase(param)) {
                        try {
                            return Long.parseLong(value) * 1000;
                        } catch (NumberFormatException ignore) {
                        }
                    }
                }
                // 保持20秒活跃
                return 20 * 1000;
            }
        };

        httpclient = HttpClientBuilder.create().setConnectionManager(
                connManager).setKeepAliveStrategy(myStrategy).build();
        shutdown = false;

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (!shutdown) {
                    try {
                        synchronized (this) {
                            wait(500);
                            // 关闭过期的连接
                            connManager.closeExpiredConnections();
                            // 关闭超过40秒的空闲连接
                            connManager.closeIdleConnections(40, TimeUnit.SECONDS);
                        }
                    } catch (Exception e) {
                        logger.error(e.getLocalizedMessage(), e);
                    }
                }
            }
        }).start();
    }

    /**
     * 基本的Get请求
     *
     * @param url            请求URL
     * @param nameValuePairs 请求List<NameValuePair>查询参数
     */
    public byte[] doGet(String url, List<NameValuePair> nameValuePairs) {
        CloseableHttpResponse response = null;
        HttpGet httpget = new HttpGet();
        try {
            URIBuilder builder = new URIBuilder(url);
            // 填入查询参数
            if (nameValuePairs != null && !nameValuePairs.isEmpty()) {
                builder.setParameters(nameValuePairs);
            }
            httpget.setURI(builder.build());
            httpget.setConfig(requestConfig);
            response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                return EntityUtils.toByteArray(entity);
            }
        } catch (Exception e) {
            logger.error("http 请求异常", e);
        } finally {
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e) {
                    logger.error("释放连接异常", e);
                }
            }
        }
        return null;
    }

    /**
     *
     * @param url
     * @param queryParams
     * @param formParams
     * @return
     */
    public byte[] doPost(String url, Map<String, String> heard, List<NameValuePair> queryParams, List<NameValuePair> formParams, String content, int timeout) {
        if (StringUtils.isBlank(url)) {
            return null;
        }
        CloseableHttpResponse response = null;
        HttpPost httppost = new HttpPost();
        try {
            URIBuilder builder = new URIBuilder(url);
            // 填入查询参数
            if (CollectionUtils.isNotEmpty(queryParams)) {
                builder.setParameters(queryParams);
            }
            httppost.setURI(builder.build());
            timeout = Optional.fromNullable(timeout).or(60000);
            requestConfig = RequestConfig.custom()
                    .setConnectTimeout(timeout).setSocketTimeout(
                            timeout).build();
            httppost.setConfig(requestConfig);
            if (null != heard) {
                for (String key : heard.keySet()) {
                    httppost.setHeader(key, heard.get(key));
                }
            }

            if (CollectionUtils.isNotEmpty(formParams)) {
                httppost.setEntity(new UrlEncodedFormEntity(formParams, "UTF-8"));
            }
            if (StringUtils.isNotBlank(content)) {
                httppost.setEntity(new StringEntity(content, "UTF-8"));
            }
            response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            int stateCode = response.getStatusLine().getStatusCode();
            if (HttpStatus.SC_OK != stateCode) {
                logger.error("非正常响应[" + stateCode + "]", new String(EntityUtils.toByteArray(entity), "UTF-8"));
                return null;
            }
            if (entity != null) {
                return EntityUtils.toByteArray(entity);
            }
        } catch (Exception e) {
            logger.error("http请求异常", e);
        } finally {
            httppost.releaseConnection();
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e) {
                    logger.error("释放连接异常", e);
                }
            }
        }
        return null;
    }

}

© 著作权归作者所有

共有 人打赏支持
WallacePang
粉丝 0
博文 4
码字总数 1448
作品 0
浦东
程序员
私信 提问
使用单例模式实现自己的HttpClient工具类

引子 在Android开发中我们经常会用到网络连接功能与服务器进行数据的交互,为此Android的SDK提供了Apache的HttpClient来方便我们使用各种Http服务。你可以把HttpClient想象成一个浏览器,通过...

moz1q1
2015/04/01
0
0
Android 浅谈HttpClient工具类

在Android开发中我们经常会用到网络连接功能与服务器进行数据的交互,为此Android的SDK提供了Apache的HttpClient来方便我们使用各种Http服务。你可以把HttpClient想象成一个浏览器,通过它的...

Jonson
2013/07/25
0
3
HttpClient/HttpUrlConnect之代理、重定向、跨域和模拟登录(session)

HttpClient和HttpUrlConnect都是java的第三方jar包,可以在maven仓库里面查询到,这两个库都是和强大的类,可以用来模拟浏览器的一些行为,从而实现网页抓取和接口调用。 这两个jar包使用其中...

陨石坠灭
11/19
0
0
一个c#的与web服务器交互的HttpClient类

.Net类库里提供了HttpWebRequest等类,方便我们编程与Web服务器进行交互. 但是实际使用中我们经常会遇到以下需求, 基础类里没有直接提供相应的功能 (WebClient类包含这些功能,只是用起来稍微麻...

鉴客
2011/12/16
3.1K
1
HttpClient-4.5总结(1)

apache httpclient不多介绍这个工具是什么,具体请看官网,不赘述。 进行记录的原因一个是把掉过坑的地方记住,另一个是自httpclient-4.4开始,官方对代码进行了很多调整,4.4以前的很多class...

李不言
2016/03/24
0
0

没有更多内容

加载失败,请刷新页面

加载更多

HashTable和Vector为什么逐渐被废弃

HashTable,不允许键值为null,还一个就是put方法使用sychronized方法进行线程同步,单线程无需同步,多线程可用concurren包的类型。 如编程思想里面说的作为工具类,封闭性做的不好没有一个...

noob_chr
昨天
0
0
Win10 下安装Win7双系统

很多人买了预装64位Win8/8.1的电脑后想重装(或者再安装一个)Win7系统,但是折腾半天发现以前的方法根本不奏效。这是因为预装Win8/8.1的电脑统一采用了UEFI+GPT引导模式,传统的BIOS(Legacy...

yaly
昨天
1
0

中国龙-扬科
昨天
1
0
假若明天来临——《AI.未来》读后感3900字

假若明天来临——《AI.未来》读后感3900字: 你有没有想过,如果有一天你被确诊为癌症患者,你会做些什么?你有没有想过,在你百年之后,你希望你的墓碑上刻写着什么内容? 在我翻开李开复老...

原创小博客
昨天
1
0
tomcat线程模型

Connector结构 BIO模式 NIO模式

grace_233
昨天
2
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部