apache httpclient 获取页面源码
HttpClient 是 Apache Jakarta Common 下的子项目,用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。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();
}
}
使用的时候除了要加入http-client.jar包之外,还需要commons-logging.jar包。
页:
[1]