前言
在上一节中我们学习了如何生成X509-P7-DER秘钥对,并使用它们用于加签和解签。本节我将学习一下如何封装一些常用的HttpClient 方法。
概述
新建Maven项目,编写一些封装代码,为了展示方便我变一次性展示写好的代码
目录
代码
AsyncHttpClientCallback.java
package Util.HttpClientUtil; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.ParseException; import org.apache.http.client.utils.HttpClientUtils; import org.apache.http.concurrent.FutureCallback; import org.apache.http.util.EntityUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; /** * 被回调的对象,给异步的httpclient使用 * */ public class AsyncHttpClientCallback implements FutureCallback<HttpResponse> { private static Logger LOG = LoggerFactory.getLogger(AsyncHttpClientCallback.class); /** * 请求完成后调用该函数 */ @Override public void completed(HttpResponse response) { LOG.warn("status:{}", response.getStatusLine().getStatusCode()); LOG.warn("response:{}", getHttpContent(response)); HttpClientUtils.closeQuietly(response); } /** * 请求取消后调用该函数 */ @Override public void cancelled() { } /** * 请求失败后调用该函数 */ @Override public void failed(Exception e) { } protected String getHttpContent(HttpResponse response) { HttpEntity entity = response.getEntity(); String body = null; if (entity == null) { return null; } try { body = EntityUtils.toString(entity, "utf-8"); } catch (ParseException e) { LOG.warn("the response's content inputstream is corrupt", e); } catch (IOException e) { LOG.warn("the response's content inputstream is corrupt", e); } return body; } }
HttpAsyncClient.java
package Util.HttpClientUtil; import org.apache.http.Consts; import org.apache.http.HttpHost; import org.apache.http.auth.AuthSchemeProvider; import org.apache.http.auth.AuthScope; import org.apache.http.auth.MalformedChallengeException; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.CredentialsProvider; import org.apache.http.client.config.AuthSchemes; import org.apache.http.client.config.RequestConfig; import org.apache.http.config.ConnectionConfig; import org.apache.http.config.Lookup; import org.apache.http.config.Registry; import org.apache.http.config.RegistryBuilder; import org.apache.http.conn.ssl.SSLContexts; import org.apache.http.impl.auth.*; import org.apache.http.impl.client.BasicCookieStore; import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.impl.nio.client.CloseableHttpAsyncClient; import org.apache.http.impl.nio.client.HttpAsyncClients; import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager; import org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor; import org.apache.http.impl.nio.reactor.IOReactorConfig; import org.apache.http.nio.conn.NoopIOSessionStrategy; import org.apache.http.nio.conn.SchemeIOSessionStrategy; import org.apache.http.nio.conn.ssl.SSLIOSessionStrategy; import org.apache.http.nio.reactor.ConnectingIOReactor; import org.apache.http.nio.reactor.IOReactorException; import javax.net.ssl.SSLContext; import java.nio.charset.CodingErrorAction; import java.security.KeyManagementException; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.security.UnrecoverableKeyException; /** * 异步的HTTP请求对象,可设置代理 */ public class HttpAsyncClient { private static int socketTimeout = 1000;// 设置等待数据超时时间5秒钟 根据业务调整 private static int connectTimeout = 2000;// 连接超时 private static int poolSize = 3000;// 连接池最大连接数 private static int maxPerRoute = 1500;// 每个主机的并发最多只有1500 private static int connectionRequestTimeout = 3000; // 从连接池中后去连接的timeout时间 // http代理相关参数 private String host = ""; private int port = 0; private String username = ""; private String password = ""; // 异步httpclient private CloseableHttpAsyncClient asyncHttpClient; // 异步加代理的httpclient private CloseableHttpAsyncClient proxyAsyncHttpClient; public HttpAsyncClient() { try { this.asyncHttpClient = createAsyncClient(false); this.proxyAsyncHttpClient = createAsyncClient(true); } catch (Exception e) { e.printStackTrace(); } } public CloseableHttpAsyncClient createAsyncClient(boolean proxy) throws KeyManagementException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException, MalformedChallengeException, IOReactorException { RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(connectionRequestTimeout) .setConnectTimeout(connectTimeout).setSocketTimeout(socketTimeout).build(); SSLContext sslcontext = SSLContexts.createDefault(); UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password); CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, credentials); // 设置协议http和https对应的处理socket链接工厂的对象 Registry<SchemeIOSessionStrategy> sessionStrategyRegistry = RegistryBuilder.<SchemeIOSessionStrategy>create() .register("http", NoopIOSessionStrategy.INSTANCE) .register("https", new SSLIOSessionStrategy(sslcontext)).build(); // 配置io线程 IOReactorConfig ioReactorConfig = IOReactorConfig.custom().setSoKeepAlive(false).setTcpNoDelay(true) .setIoThreadCount(Runtime.getRuntime().availableProcessors()).build(); // 设置连接池大小 ConnectingIOReactor ioReactor; ioReactor = new DefaultConnectingIOReactor(ioReactorConfig); PoolingNHttpClientConnectionManager conMgr = new PoolingNHttpClientConnectionManager(ioReactor, null, sessionStrategyRegistry, null); if (poolSize > 0) { conMgr.setMaxTotal(poolSize); } if (maxPerRoute > 0) { conMgr.setDefaultMaxPerRoute(maxPerRoute); } else { conMgr.setDefaultMaxPerRoute(10); } ConnectionConfig connectionConfig = ConnectionConfig.custom().setMalformedInputAction(CodingErrorAction.IGNORE) .setUnmappableInputAction(CodingErrorAction.IGNORE)