// 测试HttpClient用法
HttpClient client=new HttpClient();
//设置代理服务器地址和端口
client.getHostConfiguration().setProxy("192.168.1.235", 808);
HttpMethod method=new GetMethod("http://www.sun.com");
//使用POST方法
//HttpMethod method = new PostMethod("http://java.sun.com");
try {
client.executeMethod(method);
System.out.println(method.getStatusLine());
//打印返回的信息
// System.out.println(method.getResponseBodyAsString());
//释放连接
method.releaseConnection();
} catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
用法说明 :
导入包为:
import java.io.IOException;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
注意事项:
HttpClient 4.x于HttpClient3.X不兼容,本示例是基于HttpClient3.x的。