標籤:oid row app enc connect cep custom 內容 最大串連數
一。不帶參數的GET請求
// 建立Httpclient對象
CloseableHttpClient httpclient = HttpClients.createDefault();
// 建立http GET請求
HttpGet httpGet = new HttpGet("http://www.baidu.com/");
CloseableHttpResponse response = null;
try {
// 執行請求
response = httpclient.execute(httpGet);
// 判斷返回狀態是否為200
if (response.getStatusLine().getStatusCode() == 200) {
String content = EntityUtils.toString(response.getEntity(), "UTF-8");
System.out.println("內容長度:"+content.length());
}
} finally {
if (response != null) {
response.close();
}
httpclient.close();
}
二。帶參數的GET請求
// 建立Httpclient對象
CloseableHttpClient httpclient = HttpClients.createDefault();
// 定義請求的參數
URI uri = new URIBuilder("http://www.baidu.com/s").setParameter("wd", "java").build();
System.out.println(uri);
// 建立http GET請求
HttpGet httpGet = new HttpGet(uri);
CloseableHttpResponse response = null;
try {
// 執行請求
response = httpclient.execute(httpGet);
// 判斷返回狀態是否為200
if (response.getStatusLine().getStatusCode() == 200) {
String content = EntityUtils.toString(response.getEntity(), "UTF-8");
System.out.println(content);
}
} finally {
if (response != null) {
response.close();
}
httpclient.close();
}
三。不帶參數的POST請求
// 建立Httpclient對象
CloseableHttpClient httpclient = HttpClients.createDefault();
// 建立http POST請求
HttpPost httpPost = new HttpPost("http://www.oschina.net/");
//佈建要求頭資訊,可以偽裝成瀏覽器訪問
httpPost.setHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36");
CloseableHttpResponse response = null;
try {
// 執行請求
response = httpclient.execute(httpPost);
// 判斷返回狀態是否為200
if (response.getStatusLine().getStatusCode() == 200) {
String content = EntityUtils.toString(response.getEntity(), "UTF-8");
System.out.println(content);
}
} finally {
if (response != null) {
response.close();
}
httpclient.close();
}
四。帶參數的POST請求
// 建立Httpclient對象
CloseableHttpClient httpclient = HttpClients.createDefault();
// 建立http POST請求
HttpPost httpPost = new HttpPost("http://www.oschina.net/search");
// 設定2個post參數,一個是scope、一個是q
List<NameValuePair> parameters = new ArrayList<NameValuePair>(0);
parameters.add(new BasicNameValuePair("scope", "project"));
parameters.add(new BasicNameValuePair("q", "java"));
// 構造一個form表單式的實體
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters);
// 將請求實體設定到httpPost對象中
httpPost.setEntity(formEntity);
CloseableHttpResponse response = null;
try {
// 執行請求
response = httpclient.execute(httpPost);
// 判斷返回狀態是否為200
if (response.getStatusLine().getStatusCode() == 200) {
String content = EntityUtils.toString(response.getEntity(), "UTF-8");
System.out.println(content);
}
} finally {
if (response != null) {
response.close();
}
httpclient.close();
}
五。如果考慮效能問題,可以跟建立資料庫連接池的做法一樣,這裡建立一個串連池;例子如下:
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
// 設定最大串連數
cm.setMaxTotal(200);
// 設定每個主機地址的並發數
cm.setDefaultMaxPerRoute(20);
doGet(cm);
doGet(cm);
//這裡是調用
public static void doGet(HttpClientConnectionManager cm) throws Exception {
CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).build();
// 建立http GET請求
HttpGet httpGet = new HttpGet("http://www.baidu.com/");
CloseableHttpResponse response = null;
try {
// 執行請求
response = httpClient.execute(httpGet);
// 判斷返回狀態是否為200
if (response.getStatusLine().getStatusCode() == 200) {
String content = EntityUtils.toString(response.getEntity(), "UTF-8");
System.out.println("內容長度:" + content.length());
}
} finally {
if (response != null) {
response.close();
}
// 此處不能關閉httpClient,如果關閉httpClient,串連池也會銷毀
// httpClient.close();
}
}
HttpClient的GET請求(post)請求