文档章节

httpclient工具类

WallacePang
 WallacePang
发布于 2016/08/05 12:38
字数 641
阅读 20
收藏 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-4.5总结(1)

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

李不言
2016/03/24
0
0
研究支付时碰到了 httpclient 围观

HttpClient最重要的功能是执行HTTP方法。一个HTTP方法的执行包含一个或多个HTTP请求/HTTP响应交换,通常由HttpClient的内部来处理。而期望用户提供一个要执行的请求对象,而HttpClient期望传...

JAVA码猿
2017/11/07
0
0
Apache Client使用说明第一章(第一部分)

第一章。基础 1.1 请求的执行 HttpClient最重要的函数是用于执行HTTP方法.执行一次HTTP方法包含一次或数次HTTP请求和HTTP响应的交互,通常在httpClient内部完成.程序员只需要提供一个请求对象...

第五郎
2015/11/03
0
0

没有更多内容

加载失败,请刷新页面

加载更多

Mac OS X下Maven的安装与配置

Mac OS X 安装Maven: 下载 Maven, 并解压到某个目录。例如/Users/robbie/apache-maven-3.3.3 打开Terminal,输入以下命令,设置Maven classpath $ vi ~/.bash_profile 添加下列两行代码,之后...

TonyStarkSir
今天
3
0
关于编程,你的练习是不是有效的?

最近由于工作及Solution项目的影响,我在重新学习DDD和领域建模的一些知识。然后,我突然就想到了这个问题,以及我是怎么做的? 对于我来说,提升技能的项目会有四种: 纯兴趣驱动的项目。即...

问题终结者
今天
4
0
打开eclipse出现an error has occurred see the log file

解决方法: 1,打开eclipse安装目录下的eclipse.ini文件; 2,打开的文本文件最后添加一行 --add-modules=ALL-SYSTEM 3,保存重新打开Eclipse。...

任梁荣
昨天
4
0
搞定Northwind示例数据库,无论哪个版本的SQLServer都受用

Northwind数据库 从这里可以找到突破口: http://social.msdn.microsoft.com/Forums/zh-CN/Vsexpressvb/thread/8490a1c6-9018-40c9-aafb-df9f79d29cde 下面是MSDN: http://msdn2.microsoft......

QQZZFT
昨天
1
0
mysql主从同步,安装配置操作

准备 两台mysql服务,我这里准备了如下: 主库:192.168.176.128 从库:192.168.176.131 如何在Linux上安装mysql服务,请看https://blog.csdn.net/qq_18860653/article/details/80250499 操作...

小致dad
昨天
5
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部