1、HttpClient的pom.xml配置(maven依赖)
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.6</version>
</dependency>
2、使用PoolingHttpClientConnectionManager创建HttpClient
//使用custom message parser / writer来定制HTTP //消息被解析并写入数据流。. HttpMessageParserFactory<HttpResponse> responseParserFactory = new DefaultHttpResponseParserFactory() { @Override public HttpMessageParser<HttpResponse> create( SessionInputBuffer buffer, MessageConstraints constraints) { LineParser lineParser = new BasicLineParser() { @Override public Header parseHeader(final CharArrayBuffer buffer) { try { return super.parseHeader(buffer); } catch (ParseException ex) { return new BasicHeader(buffer.toString(), null); } } }; return new DefaultHttpResponseParser( buffer, lineParser, DefaultHttpResponseFactory.INSTANCE, constraints) { @Override protected boolean reject(final CharArrayBuffer line, int count) { //尝试无限忽略状态行前面的所有垃圾 return false; } }; } }; HttpMessageWriterFactory<HttpRequest> requestWriterFactory = new DefaultHttpRequestWriterFactory(); //使用自定义连接工厂来自定义过程 //初始化传出的HTTP连接。除了标准的连接HTTP连接工厂可以定义消息 //用于单个连接的解析器/写入器例程 HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory = new ManagedHttpClientConnectionFactory( requestWriterFactory, responseParserFactory); //客户端HTTP连接对象在完全初始化时可以绑定到 //一个任意的网络socket。网络套接字初始化的过程, //它与远程地址的连接和与本地地址的绑定是受控制的 //通过连接socket工厂。 //安全连接的SSL上下文可以基于 //系统或应用程序特定的属性。 SSLContext sslcontext = SSLContexts.createSystemDefault(); // 为所支持的自定义连接套接字工厂创建一个注册 // protocol schemes. Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create() .register("http", PlainConnectionSocketFactory.INSTANCE) .register("https", new SSLConnectionSocketFactory(sslcontext)) .build(); // 使用自定义DNS解析器覆盖系统DNS解析。 DnsResolver dnsResolver = new SystemDefaultDnsResolver() { @Override public InetAddress[] resolve(final String host) throws UnknownHostException { if (host.equalsIgnoreCase("myhost")) { return new InetAddress[] { InetAddress.getByAddress(new byte[] {127, 0, 0, 1}) }; } else { return super.resolve(host); } } }; // 使用自定义配置创建连接管理器。 PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager( socketFactoryRegistry, connFactory, dnsResolver); // 创建套接字配置 SocketConfig socketConfig = SocketConfig.custom() .setTcpNoDelay(true) .build(); //将连接管理器配置为使用socket配置 //默认或为特定的主机。 connManager.setDefaultSocketConfig(socketConfig); //connManager.setSocketConfig(new HttpHost("somehost", 80), socketConfig); // 1秒不活动后验证连接 connManager.setValidateAfterInactivity(1000); // 创建消息的约束 MessageConstraints messageConstraints = MessageConstraints.custom() .setMaxHeaderCount(200) .setMaxLineLength(2000) .build(); // 创建连接配置 ConnectionConfig connectionConfig = ConnectionConfig.custom() .setMalformedInputAction(CodingErrorAction.IGNORE) .setUnmappableInputAction(CodingErrorAction.IGNORE) .setCharset(Consts.UTF_8) .setMessageConstraints(messageConstraints) .build(); //配置连接管理器来使用连接配置 //默认或为特定的主机。 connManager.setDefaultConnectionConfig(connectionConfig); //connManager.setConnectionConfig(new HttpHost("somehost", 80), ConnectionConfig.DEFAULT); //为持久连接配置总最大或每个路由限制 //连接管理器可以在连接池中保存或租用。 connManager.setMaxTotal(100); connManager.setDefaultMaxPerRoute(10); //connManager.setMaxPerRoute(new HttpRoute(new HttpHost("somehost", 80)), 20); // 如有必要,使用自定义cookie存储。 CookieStore cookieStore = new BasicCookieStore(); // 如有必要,使用自定义证书提供程序。 CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); // 创建全局请求配置 RequestConfig defaultRequestConfig = RequestConfig.custom() .setCookieSpec(CookieSpecs.DEFAULT) .setExpectContinueEnabled(true) .setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM, AuthSchemes.DIGEST)) .setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC)) .build(); // 使用给定的自定义依赖项和配置创建HttpClient。. CloseableHttpClient httpClient = HttpClients.custom() .setConnectionManager(connManager) .setDefaultCookieStore(cookieStore) .setDefaultCredentialsProvider(credentialsProvider) //.setProxy(new HttpHost("myproxy", 8080)) .setDefaultRequestConfig(defaultRequestConfig) .build();
3、使用HttpClient执行GET和POST请求示例代码
import com.alibaba.dcm.DnsCacheManipulator; import org.apache.http.*; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.CookieStore; import org.apache.http.client.CredentialsProvider; import org.apache.http.client.config.AuthSchemes; import org.apache.http.client.config.CookieSpecs; import org.apache.http.client.config.RequestConfig; 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.config.*; import org.apache.http.conn.*; import org.apache.http.conn.HttpConnectionFactory; import org.apache.http.conn.routing.HttpRoute; import org.apache.http.conn.socket.ConnectionSocketFactory; 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.DefaultHttpResponseFactory; import org.apache.http.impl.client.BasicCookieStore; import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.conn.*; import org.apache.http.impl.io.DefaultHttpRequestWriterFactory; import org.apache.http.io.HttpMessageParser; import org.apache.http.io.HttpMessageParserFactory; import org.apache.http.io.HttpMessageWriterFactory; import org.apache.http.io.SessionInputBuffer; import org.apache.http.message.BasicHeader; import org.apache.http.message.BasicLineParser; import org.apache.http.message.LineParser; import org.apache.http.protocol.HttpContext; import org.apache.http.ssl.SSLContexts; import org.apache.http.util.CharArrayBuffer; import sun.net.InetAddressCachePolicy; import javax.net.ssl.SSLContext; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.InetAddress; import java.net.UnknownHostException; import java.nio.charset.CodingErrorAction; import java.util.Arrays; import java.util.concurrent.TimeUnit; import static java.lang.Thread.*; public class l { //创建httpClient对象 public static CloseableHttpClient getHttpClient(){ //使用custom message parser / writer来定制HTTP //消息被解析并写入数据流。. HttpMessageParserFactory<HttpResponse> responseParserFactory = new DefaultHttpResponseParserFactory() { @Override public HttpMessageParser<HttpResponse> create( SessionInputBuffer buffer, MessageConstraints constraints) { LineParser lineParser = new BasicLineParser() { @Override public Header parseHeader(final CharArrayBuffer buffer) { try { return super.parseHeader(buffer); } catch (ParseException ex) { return new BasicHeader(buffer.toString(), null); } } }; return new DefaultHttpResponseParser( buffer, lineParser, DefaultHttpResponseFactory.INSTANCE, constraints) { @Override protected boolean reject(final CharArrayBuffer line, int count) { //尝试无限忽略状态行前面的所有垃圾 return false; } }; } }; HttpMessageWriterFactory<HttpRequest> requestWriterFactory = new DefaultHttpRequestWriterFactory(); //使用自定义连接工厂来自定义过程 //初始化传出的HTTP连接。除了标准的连接HTTP连接工厂可以定义消息 //用于单个连接的解析器/写入器例程 HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory = new ManagedHttpClientConnectionFactory( requestWriterFactory, responseParserFactory); //客户端HTTP连接对象在完全初始化时可以绑定到 //一个任意的网络socket。网络套接字初始化的过程, //它与远程地址的连接和与本地地址的绑定是受控制的 //通过连接socket工厂。 //安全连接的SSL上下文可以基于 //系统或应用程序特定的属性。 SSLContext sslcontext = SSLContexts.createSystemDefault(); // 为所支持的自定义连接套接字工厂创建一个注册 // protocol schemes. Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create() .register("http", PlainConnectionSocketFactory.INSTANCE) .register("https", new SSLConnectionSocketFactory(sslcontext)) .build(); // 使用自定义DNS解析器覆盖系统DNS解析。 DnsResolver dnsResolver = new SystemDefaultDnsResolver() { @Override public InetAddress[] resolve(final String host) throws UnknownHostException { if (host.equalsIgnoreCase("myhost")) { return new InetAddress[] { InetAddress.getByAddress(new byte[] {127, 0, 0, 1}) }; } else { return super.resolve(host); } } }; // 使用自定义配置创建连接管理器。 PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager( socketFactoryRegistry, connFactory, dnsResolver); // 创建套接字配置 SocketConfig socketConfig = SocketConfig.custom() .setTcpNoDelay(true) .build(); //将连接管理器配置为使用socket配置 //默认或为特定的主机。 connManager.setDefaultSocketConfig(socketConfig); //connManager.setSocketConfig(new HttpHost("somehost", 80), socketConfig); // Validate connections after 1 sec of inactivity connManager.setValidateAfterInactivity(1000); // 创建消息的约束 MessageConstraints messageConstraints = MessageConstraints.custom() .setMaxHeaderCount(200) .setMaxLineLength(2000) .build(); // 创建连接配置 ConnectionConfig connectionConfig = ConnectionConfig.custom() .setMalformedInputAction(CodingErrorAction.IGNORE) .setUnmappableInputAction(CodingErrorAction.IGNORE) .setCharset(Consts.UTF_8) .setMessageConstraints(messageConstraints) .build(); //配置连接管理器来使用连接配置 //默认或为特定的主机。 connManager.setDefaultConnectionConfig(connectionConfig); //connManager.setConnectionConfig(new HttpHost("somehost", 80), ConnectionConfig.DEFAULT); //为持久连接配置总最大或每个路由限制 //连接管理器可以在连接池中保存或租用。 connManager.setMaxTotal(100); connManager.setDefaultMaxPerRoute(10); //connManager.setMaxPerRoute(new HttpRoute(new HttpHost("somehost", 80)), 20); // 如有必要,使用自定义cookie存储。 CookieStore cookieStore = new BasicCookieStore(); // 如有必要,使用自定义证书提供程序。 CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); // 创建全局请求配置 RequestConfig defaultRequestConfig = RequestConfig.custom() .setCookieSpec(CookieSpecs.DEFAULT) .setExpectContinueEnabled(true) .setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM, AuthSchemes.DIGEST)) .setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC)) .build(); // 使用给定的自定义依赖项和配置创建HttpClient。. CloseableHttpClient httpClient = HttpClients.custom() .setConnectionManager(connManager) .setDefaultCookieStore(cookieStore) .setDefaultCredentialsProvider(credentialsProvider) //.setProxy(new HttpHost("myproxy", 8080)) .setDefaultRequestConfig(defaultRequestConfig) .build(); return httpClient; } /** * 模拟发送url Get 请求 * * @param url * @return */ public static String requestGetUrl(String url) { CloseableHttpClient httpClient = HttpClients.createDefault(); StringBuilder entityStringBuilder = null; try { HttpGet get = new HttpGet(url); CloseableHttpResponse httpResponse = null; httpResponse = httpClient.execute(get); try { HttpEntity entity = httpResponse.getEntity(); entityStringBuilder = new StringBuilder(); if (null != entity) { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(), "UTF-8"), 8 * 1024); String line = null; while ((line = bufferedReader.readLine()) != null) { entityStringBuilder.append(line + "/n"); } } } finally { httpResponse.close(); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (httpClient != null) { httpClient.close(); } } catch (IOException e) { e.printStackTrace(); } } return entityStringBuilder.toString(); } //执行post请求 public static String requestPostUrl(String url, String param, String token) throws Exception { InputStream is = null; String body = null; StringBuilder res = new StringBuilder(); HttpPost httpPost = new HttpPost(url); httpPost.addHeader("Authorization", "Token " + token); httpPost.addHeader("Content-Type", "application/json"); StringEntity stringEntity = new StringEntity(param, "utf-8"); stringEntity.setContentEncoding("UTF-8"); stringEntity.setContentType("application/json"); // post.setHeaders(headers); //代headers请求 httpPost.setEntity(stringEntity); RequestConfig config = RequestConfig.custom().setConnectTimeout(5000).build(); httpPost.setConfig(config); CloseableHttpClient httpClient = getHttpClient(); // HttpClients.createDefault(); CloseableHttpResponse response = httpClient.execute(httpPost); HttpEntity entity = response.getEntity(); if (entity != null) { is = entity.getContent(); //转换为字节输入流 BufferedReader br = new BufferedReader(new InputStreamReader(is, Consts.UTF_8)); while ((body = br.readLine()) != null) { res.append(body); } } System.out.println(res.toString()); //Object jsonMap = JSON.parse(res.toString()); return res.toString(); } public static void main(String[] args) throws UnknownHostException, InterruptedException { try { String result1 = requestGetUrl("http: //example.com:8090/index/2/"); System.out.println(result1); String result2 = requestPostUrl("http: //example.com:8090/index/2/","cjavapy","d-a613-b1b1ae807064"); System.out.println(result2); } catch (Exception e) { e.printStackTrace(); } } }