HttpClient的get方式和post方式詳解
阿新 • • 發佈:2019-02-17
使用 HttpClient 需要以下 6 個步驟:
1. 建立 HttpClient 的例項
2. 建立某種連線方法的例項,在這裡是 GetMethod。在 GetMethod 的建構函式中傳入待連線的地址
3. 呼叫第一步中建立好的例項的 execute 方法來執行第二步中建立好的 method 例項
4. 讀 response
5. 釋放連線。無論執行方法是否成功,都必須釋放連線
6. 對得到後的內容進行處理
根據以上步驟,我們來編寫用GET方法來取得某網頁內容的程式碼。
- 大部分情況下 HttpClient 預設的建構函式已經足夠使用。
HttpClient httpClient = new HttpClient();
- 建立GET方法的例項。在GET方法的建構函式中傳入待連線的地址即可。用GetMethod將會自動處理轉發過程,如果想要把自動處理轉發過程去掉的話,可以呼叫方法setFollowRedirects(false)。
GetMethod getMethod = new GetMethod("http://www.ibm.com/");
- 呼叫例項httpClient的executeMethod方法來執行getMethod。由於是執行在網路上的程式,在執行executeMethod方法的時候,需要處理兩個異常,分別是HttpException和IOException。引起第一種異常的原因主要可能是在構造getMethod的時候傳入的協議不對,比如不小心將"http"寫成"htp",或者伺服器端返回的內容不正常等,並且該異常發生是不可恢復的;第二種異常一般是由於網路原因引起的異常,對於這種異常 (IOException),HttpClient會根據你指定的恢復策略自動試著重新執行executeMethod方法。HttpClient的恢復策略可以自定義(通過實現介面HttpMethodRetryHandler來實現)。通過httpClient的方法setParameter設定你實現的恢復策略,本文中使用的是系統提供的預設恢復策略,該策略在碰到第二類異常的時候將自動重試3次。executeMethod返回值是一個整數,表示了執行該方法後伺服器返回的狀態碼,該狀態碼能表示出該方法執行是否成功、需要認證或者頁面發生了跳轉(預設狀態下GetMethod的例項是自動處理跳轉的)等。
//設定成了預設的恢復策略,在發生異常時候將自動重試3次,在這裡你也可以設定成自定義的恢復策略 getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); //執行getMethod int statusCode = client.executeMethod(getMethod); if (statusCode != HttpStatus.SC_OK) { System.err.println("Method failed: " + getMethod.getStatusLine()); }
- 在返回的狀態碼正確後,即可取得內容。取得目標地址的內容有三種方法:第一種,getResponseBody,該方法返回的是目標的二進位制的byte流;第二種,getResponseBodyAsString,這個方法返回的是String型別,值得注意的是該方法返回的String的編碼是根據系統預設的編碼方式,所以返回的String值可能編碼型別有誤,在本文的"字元編碼"部分中將對此做詳細介紹;第三種,getResponseBodyAsStream,這個方法對於目標地址中有大量資料需要傳輸是最佳的。在這裡我們使用了最簡單的getResponseBody方法。
byte[] responseBody = method.getResponseBody();
- 釋放連線。無論執行方法是否成功,都必須釋放連線。
method.releaseConnection();
- 處理內容。在這一步中根據你的需要處理內容,在例子中只是簡單的將內容列印到控制檯。
System.out.println(new String(responseBody));
下面是程式的完整程式碼,這些程式碼也可在附件中的test.GetSample中找到。
package test;
import java.io.IOException;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
public class GetSample{
public static void main(String[] args) {
//構造HttpClient的例項
HttpClient httpClient = new HttpClient();
//建立GET方法的例項
GetMethod getMethod = new GetMethod("http://www.ibm.com");
//使用系統提供的預設的恢復策略
getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler());
try {
//執行getMethod
int statusCode = httpClient.executeMethod(getMethod);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: "
+ getMethod.getStatusLine());
}
//讀取內容
byte[] responseBody = getMethod.getResponseBody();
//處理內容
System.out.println(new String(responseBody));
} catch (HttpException e) {
//發生致命的異常,可能是協議不對或者返回的內容有問題
System.out.println("Please check your provided http address!");
e.printStackTrace();
} catch (IOException e) {
//發生網路異常
e.printStackTrace();
} finally {
//釋放連線
getMethod.releaseConnection();
}
}
}
下面這個是本人實用的demo
package com.bw.util;
import java.io.IOException;
import java.net.URI;
import java.util.Map;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
/**
* httpClient 使用java程式碼模擬瀏覽器來發出http請求,並獲取響應結果
*
*/
public class HttpClientUtil {
public static void main(String[] args) {
String url = "http://127.0.0.1:9090/taotao_rest/rest/item/536563";
String result = doGet(url, null);
System.out.println(result);
}
//
public static String doGet(String url,Map<String, String> paramMap) {
CloseableHttpResponse response = null;
CloseableHttpClient httpClient = null;
String result = null;
try {
//1.建立httpClient物件
httpClient = HttpClients.createDefault();
//2.建立uri
URIBuilder uriBuilder = new URIBuilder(url);
if(paramMap != null && !paramMap.isEmpty()) {
for (String key : paramMap.keySet()) {
uriBuilder.addParameter(key, paramMap.get(key));
}
}
URI uri = uriBuilder.build();
//3.建立HttpGet
HttpGet httpGet = new HttpGet(uri);
//4.傳送http請求
response = httpClient.execute(httpGet);
//5.獲取響應結果
if(response != null && response.getStatusLine().getStatusCode() == 200) {
result = EntityUtils.toString(response.getEntity(), "utf-8");
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
//6.關閉資源
if(response != null) {
response.close();
}
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
}
post 請求
根據RFC2616,對POST的解釋如下:POST方法用來向目的伺服器發出請求,要求它接受被附在請求後的實體,並把它當作請求佇列(Request-Line)中請求URI所指定資源的附加新子項。POST被設計成用統一的方法實現下列功能:
- 對現有資源的註釋(Annotation of existing resources)
- 向電子公告欄、新聞組,郵件列表或類似討論組傳送訊息
- 提交資料塊,如將表單的結果提交給資料處理過程
- 通過附加操作來擴充套件資料庫
呼叫HttpClient中的PostMethod與GetMethod類似,除了設定PostMethod的例項與GetMethod有些不同之外,剩下的步驟都差不多。在下面的例子中,省去了與GetMethod相同的步驟,只說明與上面不同的地方,並以登入清華大學BBS為例子進行說明。
- 構造PostMethod之前的步驟都相同,與GetMethod一樣,構造PostMethod也需要一個URI引數,在本例中,登入的地址是http://www.newsmth.net/bbslogin2.php。在建立了PostMethod的例項之後,需要給method例項填充表單的值,在BBS的登入表單中需要有兩個域,第一個是使用者名稱(域名叫id),第二個是密碼(域名叫passwd)。表單中的域用類NameValuePair來表示,該類的建構函式第一個引數是域名,第二引數是該域的值;將表單所有的值設定到PostMethod中用方法setRequestBody。另外由於BBS登入成功後會轉向另外一個頁面,但是HttpClient對於要求接受後繼服務的請求,比如POST和PUT,不支援自動轉發,因此需要自己對頁面轉向做處理。具體的頁面轉向處理請參見下面的"自動轉向"部分。程式碼如下:
String url = "http://www.newsmth.net/bbslogin2.php";
PostMethod postMethod = new PostMethod(url);
// 填入各個表單域的值
NameValuePair[] data = { new NameValuePair("id", "youUserName"),
new NameValuePair("passwd", "yourPwd") };
// 將表單的值放入postMethod中
postMethod.setRequestBody(data);
// 執行postMethod
int statusCode = httpClient.executeMethod(postMethod);
// HttpClient對於要求接受後繼服務的請求,象POST和PUT等不能自動處理轉發
// 301或者302
if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY ||
statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
// 從頭中取出轉向的地址
Header locationHeader = postMethod.getResponseHeader("location");
String location = null;
if (locationHeader != null) {
location = locationHeader.getValue();
System.out.println("The page was redirected to:" + location);
} else {
System.err.println("Location field value is null.");
}
return;
下面還有HttpClient中常遇到的問題,就不展示了,需要了解的可以去看原文章
原文網址為:https://blog.csdn.net/x_yp/article/details/6313651