您的位置:首页 > 理论基础 > 计算机网络

Java HttpClient 网络爬虫怎么使用

2015-10-06 21:17 537 查看
1,解压httpcomponents-client-4.2.5-bin.zip

拷贝依赖包:

commons-codec-1.6.jar

commons-logging-1.1.1.jar

fluent-hc-4.2.5.jar

httpclient-4.2.5.jar

httpclient-cache-4.2.5.jar

httpcore-4.2.4.jar

httpmime-4.2.5.jar

2,写一个Java类

package com.chuangfuwu.httpclient;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class Test {

public static void main(String[] args) throws ClientProtocolException, IOException {
HttpGet httpGet = new HttpGet("http://www.baidu.com");
HttpClient httpClient=new DefaultHttpClient();
HttpResponse httpResponse = httpClient.execute(httpGet);
String htmlString=EntityUtils.toString(httpResponse.getEntity());
System.out.println(htmlString);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: