Posted on 2005-04-04 11:18
风太少 阅读(735)
评论(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));
}
}