HttpClient最重要的功能是執行HTTP方法。一個HTTP方法的執行包含一個或多個HTTP請求/HTTP響應交換,一般由HttpClient的內部來處理。而指望用戶提供一個要執行的請求對象,而HttpClient指望傳輸請求到目標服務器並返回對應的響應對象,或者當執行不成功時拋出異常。javascript
interface HttpClient ,html
實現類有AbstractHttpClient、AndroidHttpClientjava
DefaultHttpClient extends AbstractHttpClientapache
一個簡單的HttpClient執行例子編程
HttpClient httpclient = new DefaultHttpClient();瀏覽器
HttpGet httpget = new HttpGet("http://localhost/");緩存
HttpResponse response = httpclient.execute(httpget);服務器
HttpEntity entity = response.getEntity();dom
if (entity != null) {工具
InputStream instream = entity.getContent();
int l;
byte[] tmp = new byte[2048];
while ((l = instream.read(tmp)) != -1) {
}
}
/**
* 爲HttpClient添加請求超時時間
* @return
*/
public HttpClient getHttpClient(){
BasicHttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 5000); //設置請求超時10秒鐘
HttpConnectionParams.setSoTimeout(httpParams, 5000); //設置等待數據超時時間10秒鐘
HttpClient client = new DefaultHttpClient(httpParams);
return client;
}
HttpClient最重要的功能是執行HTTP方法。一個HTTP方法的執行包含一個或多個HTTP請求/HTTP響應交換,一般由HttpClient的內部來處理。而指望用戶提供一個要執行的請求對象,而HttpClient指望傳輸請求到目標服務器並返回對應的響應對象,或者當執行不成功時拋出異常。
很天然地,HttpClient API的主要切入點就是定義描述上述規約的HttpClient接口。
這裏有一個很簡單的請求執行過程的示例:
HttpClient httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet("http://localhost/"); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); if (entity != null) { InputStream instream = entity.getContent(); int l; byte[] tmp = new byte[2048]; while ((l = instream.read(tmp)) != -1) { } }
全部HTTP請求有一個組合了方法名,請求URI和HTTP協議版本的請求行。
HttpClient支持全部定義在HTTP/1.1版本中的HTTP方法:GET,HEAD,POST,PUT,DELETE,TRACE和OPTIONS。對於每一個方法類型都有一個特殊的類:HttpGet,HttpHead,HttpPost,HttpPut,HttpDelete,HttpTrace和HttpOptions。
請求的URI是統一資源定位符,它標識了應用於哪一個請求之上的資源。HTTP請求URI包含一個協議模式,主機名稱,可選的端口,資源路徑,可選的查詢和可選的片斷。
HttpGet httpget = new HttpGet( "http://www.google.com/search?hl=en&q=httpclient&btnG=Google+Search&aq=f&oq=");
HttpClient提供不少工具方法來簡化建立和修改執行URI。
URI也能夠編程來拼裝:
URI uri = URIUtils.createURI("http", "www.google.com", -1, "/search", "q=httpclient&btnG=Google+Search&aq=f&oq=", null); HttpGet httpget = new HttpGet(uri); System.out.println(httpget.getURI());
輸出內容爲:
http://www.google.com/search?q=httpclient&btnG=Google+Search&aq=f&oq=
查詢字符串也能夠從獨立的參數中來生成:
List<NameValuePair> qparams = new ArrayList<NameValuePair>(); qparams.add(new BasicNameValuePair("q", "httpclient")); qparams.add(new BasicNameValuePair("btnG", "Google Search")); qparams.add(new BasicNameValuePair("aq", "f")); qparams.add(new BasicNameValuePair("oq", null)); URI uri = URIUtils.createURI("http", "www.google.com", -1, "/search", URLEncodedUtils.format(qparams, "UTF-8"), null); HttpGet httpget = new HttpGet(uri); System.out.println(httpget.getURI());
輸出內容爲:
http://www.google.com/search?q=httpclient&btnG=Google+Search&aq=f&oq=
HTTP響應是由服務器在接收和解釋請求報文以後返回發送給客戶端的報文。響應報文的第一行包含了協議版本,以後是數字狀態碼和相關聯的文本段。
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"); System.out.println(response.getProtocolVersion()); System.out.println(response.getStatusLine().getStatusCode()); System.out.println(response.getStatusLine().getReasonPhrase()); System.out.println(response.getStatusLine().toString());
輸出內容爲:
HTTP/1.1
200
OK
HTTP/1.1 200 OK
一個HTTP報文能夠包含不少描述如內容長度,內容類型等信息屬性的頭部信息。
HttpClient提供獲取,添加,移除和枚舉頭部信息的方法。
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
HttpStatus.SC_OK, "OK");
response.addHeader("Set-Cookie",
"c1=a; path=/; domain=localhost");
response.addHeader("Set-Cookie",
"c2=b; path=\"/\", c3=c; domain=\"localhost\"");
Header h1 = response.getFirstHeader("Set-Cookie");
System.out.println(h1);
Header h2 = response.getLastHeader("Set-Cookie");
System.out.println(h2);
Header[] hs = response.getHeaders("Set-Cookie");
System.out.println(hs.length);
輸出內容爲:
Set-Cookie: c1=a; path=/; domain=localhost
Set-Cookie: c2=b; path="/", c3=c; domain="localhost"
得到給定類型的全部頭部信息最有效的方式是使用HeaderIterator接口。
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
HttpStatus.SC_OK, "OK");
response.addHeader("Set-Cookie",
"c1=a; path=/; domain=localhost");
response.addHeader("Set-Cookie",
"c2=b; path=\"/\", c3=c; domain=\"localhost\"");
HeaderIterator it = response.headerIterator("Set-Cookie");
while (it.hasNext()) {
System.out.println(it.next());
}
輸出內容爲:
Set-Cookie: c1=a; path=/; domain=localhost
Set-Cookie: c2=b; path="/", c3=c; domain="localhost"
它也提供解析HTTP報文到獨立頭部信息元素的方法方法。
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
HttpStatus.SC_OK, "OK");
response.addHeader("Set-Cookie",
"c1=a; path=/; domain=localhost");
response.addHeader("Set-Cookie",
"c2=b; path=\"/\", c3=c; domain=\"localhost\"");
HeaderElementIterator it = new BasicHeaderElementIterator(
response.headerIterator("Set-Cookie"));
while (it.hasNext()) {
HeaderElement elem = it.nextElement();
System.out.println(elem.getName() + " = " + elem.getValue());
NameValuePair[] params = elem.getParameters();
for (int i = 0; i < params.length; i++) {
System.out.println(" " + params[i]);
}
}
輸出內容爲:
c1 = a
path=/
domain=localhost
c2 = b
path=/
c3 = c
domain=localhost
暫時先整理到這裏,附上轉載連接:http://blog.csdn.net/yujian_bing/article/details/8174572