HttpClient 是 Apache Jakarta Common 下的子项目,用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。项目首页是http://hc.apache.org/httpclient-3.x/,下载地址:http://hc.apache.org/downloads.cgi,本人下载的是 HttpClient 4.0.1 (GA)
首先声明一下,下载包里面有现成的例子,貌似4.0 和以前的版本差别很大,因为在网上找的别的例子,在4.0版本里面连一些类都找不到了
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.impl.client.DefaultHttpClient;
-
- public class ClientAbortMethod {
-
- public final static void main(String[] args) throws Exception {
- HttpClient httpclient = new DefaultHttpClient();
-
- HttpGet httpget = new HttpGet("http://www.apache.org/");
-
- System.out.println("executing request " + httpget.getURI());
- HttpResponse response = httpclient.execute(httpget);
- HttpEntity entity = response.getEntity();
-
- System.out.println("----------------------------------------");
- System.out.println(response.getStatusLine());
- if (entity != null) {
- BufferedReader reader = new BufferedReader(
- new InputStreamReader(entity.getContent()));
- String str = null;
- if(null != (str = reader.readLine()) ){
- System.out.println(str);
- }
-
- }
- System.out.println("----------------------------------------");
-
- httpget.abort();
-
- httpclient.getConnectionManager().shutdown();
- }
-
- }
作用:可以搭配使用在短信发送等功能上。
代码中有未引入的类和异常,大家自己处理一下就OK了
如此便可打印出网页所有源代码,当然这只是最简单的一种情况
使用的时候除了要加入http-client.jar包之外,还需要commons-logging.jar包,大家直接从lib文件夹下面copy进去就OK了