Posted on 2005-04-04 11:18
風太少 閱讀(739)
評論(0) 編輯 收藏
SECTION 01 HttpClient 總覽
Hyper-Text Transfer Protocol (HTTP) 是現在網絡上最通行的通訊協議, 隨著 Webservice 的技術快速發展, 也讓許多的應用程序結合 HTTP 擴展更大的功能, 或許你會說 java.net 不是已經有基本的存取 HTTP 的 method 了, 為何還要有這個 commons 的項目呢, 請容許我慢慢說明 ~~
binary 下載處
http://jakarta.apache.org/builds/jakarta-commons/release/commons-httpclient/v2.0/ source 下載處
http://www.apache.org/dist/jakarta/commons/httpclient/source/ SECTION 02 特色
是因為他的特色..... 有許多是 java.net.* 沒有提供的, 或者自己要實現的. 不如就直接采用 commons-httpclient.
- 使用 Pure Java 開發標準的 HTTP v1.0 及 v1.1
- 實現所有的 HTTP methods (GET, POST, PUT, DELETE,HEAD, OPTIONS, and TRACE)
- 支持 HTTPS 的協議
- 支持 proxies 的各種情況
- 利用 Multi Form 上傳文件
- 支持認證機制
- 可以設置最大連結數量
- 自動的 cookie 處理模式
- Request 及 Response 最佳化處理
- 支持 HTTP 1.0 KeepAlive 聯機模式及 1.1 的 persistance 保存狀態
- 直接存取服務器送來的 response code 及 header
- 能夠設置連結超時
- 實現 Command Pattern 允許去平行處理及有效重復使用連結.
- 這個是開放源碼的
SECTION 03 依循自
Commons-HttpClient 是遵循了下面的 spec ( Internet Engineering Task Force (IETF) )
- RFC1945Hypertext Transfer Protocol -- HTTP/1.0
- RFC2616Hypertext Transfer Protocol -- HTTP/1.1
- RFC2617HTTP Authentication: Basic and Digest Access Authentication
- RFC2109HTTP State Management Mechanism (Cookies)
- RFC2396Uniform Resource Identifiers (URI): Generic Syntax
- RFC1867Form-based File Upload in HTML
SECTION 04 與其它的 HttpClient 比較
此來源出自
http://www.nogoop.com/product_16.html#compare
|
nogoop |
Sun |
innovation |
Jakarta |
Features |
cookies |
|
|
X |
X |
plug compatible |
X |
X |
X |
[partial] |
true request output stream |
|
|
X |
X |
true response input stream |
X |
|
X |
X |
connection keep alive |
X |
X |
X |
X |
connection pool throttling |
X |
|
|
X |
connection/request timeout |
X |
|
X |
X |
idle connection timeout |
X |
|
|
|
pipelining of requests |
|
|
X |
|
SSL |
X |
X |
X |
X |
basic authentication |
X |
X |
X |
X |
digest authentication |
X |
X |
X |
X |
NTLM authentication |
X |
|
|
X |
proxy authentication |
X |
X |
X |
X |
Support |
minimum JRE version |
1.2 |
1.0 |
1.2 |
1.2 |
price |
$399 |
free |
free |
free |
source available |
X |
|
X |
X |
diagnostic tracing |
X |
|
X |
X |
actively supported |
X |
X |
|
X |
fix turnaround |
fast |
slow |
none |
medium |
license |
purchase |
Sun JRE |
LGPL |
Apache |
SECTION 05 簡單范例
基本上 HttpClient 需要 commons-logging, 所以你要在 classpath 設置這兩個 jar 文件, 接著你要了解 HTTP 通訊的方式, 標準步驟如下..
- 建立 HttpClient 的 instance ( HttpClient client = new HttpClient(); )
- 建立 HttpClient instance 要使用的 method ( 例如 GET/POST 等等 連結到一個 URL , HttpMethod method = new GetMethod("http://www.apache.org/"); )
- 告訴 HttpClient 去執行這個訊息 ( statusCode = client.executeMethod(method); )
- 得到 Server 端的響應 ( byte[] responseBody = method.getResponseBody(); )
- 結束這個連結 ( method.releaseConnection(); )
- 處理這個響應數據 ( System.err.println(new String(responseBody)); )
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import java.io.*;
public class HttpClientTutorial {
private static String url = "http://www.apache.org/";
public static void main(String[] args) {
// Create an instance of HttpClient.
HttpClient client = new HttpClient();
// Create a method instance.
HttpMethod method = new GetMethod(url);
// Execute the method.
int statusCode = -1;
int attempt = 0;
// We will retry up to 3 times.
while (statusCode == -1 && attempt < 3) {
try {
// execute the method.
statusCode = client.executeMethod(method);
} catch (HttpRecoverableException e) {
System.err.println(
"A recoverable exception occurred, retrying." +
e.getMessage());
} catch (IOException e) {
System.err.println("Failed to download file.");
e.printStackTrace();
System.exit(-1);
}
}
// Check that we didn't run out of retries.
if (statusCode == -1) {
System.err.println("Failed to recover from exception.");
System.exit(-2);
}
// Read the response body.
byte[] responseBody = method.getResponseBody();
// Release the connection.
method.releaseConnection();
// Deal with the response.
// Use caution: ensure correct character encoding and is not binary data
System.err.println(new String(responseBody));
}
}