参考:http://www.blogjava.net/alpha/archive/2007/01/22/95216.html
http://www.360doc.com/content/09/1201/18/203871_10149531.shtml
HttpClient是apache设计实现,用来简化http客户端与服务器端的网络通信编程接口。目前,有commons-httpclient和httpclient俩个版本,前者最后版本是3.1,后者分为HttpClient和HttpCore两个部分,同时包含server和client端的API,版本号从4.0开始。我们尽量优先使用最新版本的httpclient。
commons-httpclient
1、GET/POST
a、get/post
private static final String host = "localhost";
private static final int port = 8080;
HttpClient client = new HttpClient();
client.getHostConfiguration.setHost(host,port,"http");
//get方式提交信息
GetMethod getMethod = new GetMethod("/index.jsp?key=520");
//获取流
InputStream in = getMethod.getResponseBodyAsStream();
//post方式提交信息
PostMethod postMethod = new PostMethod("/index.jsp");
NameValuePair value1 = new NameValuePair("name","felix");
NameValuePair value2 = new NameValuePair("age",18);
postMethod.setRequestBody(new NameValuePair[][value1,value2]);
client.executeMethod(getMethod);
client.executeMethod(postMethod);
//记得释放连接哦
getMethod.releaseConnection();
postMethod.releaseConnection();
b、head
HeadMethod head = new HeadMethod("http://www.csdn.net");
// 执行方法,并处理失败的请求.
...
// 取回应答包的头字段信息.
Header[] headers = head.getResponseHeaders();
// 只取回最后修改日期字段的信息.
String lastModified = head.getResponseHeader("last-modified").getValue();
c、post (使用HttpPost)
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost postMethod = new HttpPost("http://reg.