看本身動手寫爬蟲,沒想到一上來就跪了。java
裏面提到用的jar包是apache的http客戶端開源項目---HttpClientapache
就去官網下載了一個版本4.3ide
當按書上代碼敲時google
HttpClient httpclient = new HttpClient();url
敲完這句,就給跪了spa
提示Cannot instantiate the type HttpClient,code
google 了下,在stackoverflow上面說是應該blog
HttpClient httpclient = new DefaultHttpClient();get
這樣寫,不過得先import org.apache.http.impl.client.DefaultHttpClient;it
試了一下。。。果真能夠,可是後面的GetMethod啥的都有,
最後才特麼發現從4.×版本後,它的用法就變了不能這麼使用了
給個官網示例看看就知道咋回事了
package spider; import java.io.IOException; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.ResponseHandler; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; public class Spider { public static void main(String[] args) throws Exception{ CloseableHttpClient httpclient = HttpClients.createDefault(); try{ String url = "http://www.baidu.com"; HttpGet httpGet = new HttpGet(url); System.out.println("executing request " + httpGet.getURI()); ResponseHandler<String> responseHandler = new ResponseHandler<String>(){ public String handleResponse(final HttpResponse response) throws ClientProtocolException,IOException{ int status = response.getStatusLine().getStatusCode(); if (status >= 200 && status < 300){ HttpEntity entity = response.getEntity(); return entity !=null ? EntityUtils.toString(entity) : null; }else{ throw new ClientProtocolException("Unexpected response status: " + status); } } }; String responseBody = httpclient.execute(httpGet,responseHandler); System.out.println("-------------------------------------------"); System.out.println(responseBody); System.out.println("-------------------------------------------"); }finally{ httpclient.close(); } } }