HttpClient的连接池||HttpClient的请求参数
生活随笔
收集整理的這篇文章主要介紹了
HttpClient的连接池||HttpClient的请求参数
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
連接池
如果每次請(qǐng)求都要?jiǎng)?chuàng)建HttpClient,會(huì)有頻繁創(chuàng)建和銷(xiāo)毀的問(wèn)題,可以使用連接池來(lái)解決這個(gè)問(wèn)題
HttpClientPoolTest.java
package cn.itcast.crawler.test;import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.apache.http.util.EntityUtils;import java.io.IOException;public class HttpClientPoolTest {public static void main(String[] args) {//創(chuàng)建連接池管理器PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();//設(shè)置最大連接數(shù)cm.setMaxTotal(100);//設(shè)置每個(gè)主機(jī)的最大連接數(shù)cm.setDefaultMaxPerRoute(10);//使用連接池管理器發(fā)起請(qǐng)求doGet(cm);doGet(cm);}private static void doGet(PoolingHttpClientConnectionManager cm) {//不是每次創(chuàng)建新的HttpClient,而是從連接池中獲取HttpClient對(duì)象CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).build();HttpGet httpGet = new HttpGet("http://www.itcast.cn");CloseableHttpResponse response = null;try {response = httpClient.execute(httpGet);if (response.getStatusLine().getStatusCode() == 200) {String content = EntityUtils.toString(response.getEntity(), "utf8");System.out.println(content.length());}} catch (IOException e) {e.printStackTrace();}finally {if (response != null) {try {response.close();} catch (IOException e) {e.printStackTrace();}//不能關(guān)閉HttpClient,由連接池管理HttpClient//httpClient.close();}}} }HttpClient的請(qǐng)求參數(shù)
有時(shí)候因?yàn)榫W(wǎng)絡(luò),或者目標(biāo)服務(wù)器的原因,請(qǐng)求需要更長(zhǎng)的時(shí)間才能完成,我們需要自定義相關(guān)時(shí)間
HttpConfigTest.java
package cn.itcast.crawler.test;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.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils;import java.io.IOException;public class HttpConfigTest {public static void main(String[] args) {//創(chuàng)建HttpClient對(duì)象CloseableHttpClient httpClient = HttpClients.createDefault();//創(chuàng)建HttpGet對(duì)象,設(shè)置url訪問(wèn)地址HttpGet httpGet = new HttpGet("http://www.itcast.cn");//配置請(qǐng)求信息RequestConfig config = RequestConfig.custom().setConnectTimeout(1000) //創(chuàng)建連接的最長(zhǎng)時(shí)間,單位是毫秒.setConnectionRequestTimeout(500) //設(shè)置獲取連接的最長(zhǎng)時(shí)間,單位是毫秒.setSocketTimeout(10*1000) //設(shè)置數(shù)據(jù)傳輸?shù)淖铋L(zhǎng)時(shí)間,單位是毫秒.build();//給請(qǐng)求設(shè)置請(qǐng)求信息httpGet.setConfig(config);CloseableHttpResponse response = null;try {//使用HttpClient發(fā)起請(qǐng)求,獲取responseresponse = httpClient.execute(httpGet);//解析響應(yīng)if (response.getStatusLine().getStatusCode() == 200) {String content = EntityUtils.toString(response.getEntity(), "utf8");System.out.println(content.length());}} catch (IOException e) {e.printStackTrace();}finally {//關(guān)閉responsetry {response.close();} catch (IOException e) {e.printStackTrace();}try {httpClient.close();} catch (IOException e) {e.printStackTrace();}}} }?
總結(jié)
以上是生活随笔為你收集整理的HttpClient的连接池||HttpClient的请求参数的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: POST请求||带参数的POST请求
- 下一篇: Jsoup介绍||jsou