HttpClient的GET請求(post)請求

來源:互聯網
上載者:User

標籤: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)請求

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.