Cannot instantiate the type HttpClient问题
看自己动手写爬虫,没想到一上来就跪了。
里面提到用的jar包是apache的http客户端开源项目---HttpClient
就去官网下载了一个版本4.3
当按书上代码敲时
HttpClient httpclient = new HttpClient();
敲完这句,就给跪了
提示Cannot instantiate the type HttpClient,
google 了下,在stackoverflow上面说是应该
HttpClient httpclient = new DefaultHttpClient();
这样写,不过得先import org.apache.http.impl.client.DefaultHttpClient;
试了一下。。。果然可以,但是后面的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();
}
}
}
Cannot instantiate the type HttpClient问题的更多相关文章
- Cannot instantiate the type List<Integer>
在使用java.util.List; 的时候,把语句写成了: List<Integer> arr = new List<Integer>(); 导致错误: Cannot ins ...
- Cannot instantiate the type Map
今天在使用Map中犯了个低级错误,记录一下: 打算使用map为一个视频List观看记录的统计标识,key为vid,value默认为false,当该key已经统计,标识value为true,初始实例化M ...
- Cannot instantiate the type AppiumDriver
I have added following jars in my projects build path: java-client-2.0.0 from http://appium.io/downl ...
- spring mvc绑定复杂对象报错“Could not instantiate property type [com.ld.net.spider.pojo.WorkNode] to auto-grow nested property path: java.lang.InstantiationException: com.ld.net.spider.pojo.WorkNode”
解决方法之一: 1.确保所有的Pojo都包含了默认的构造器:
- AppiumDriver升级到2.0.0版本引发的问题--Cannot instantiate the type AppiumDriver
1. 问题描述和起因 在使用Appium1.7.0及其以下版本的时候,我们可以直接使用如下代码来创建一个AppiumDriver实例进行对安卓设备的操作. driver = new AndroidDr ...
- Cannot instantiate the type AppiumDriver,AppiumDriver升级引发的问题
转自:http://blog.csdn.net/zhubaitian/article/details/39717889 1. 问题描述和起因 在使用Appium1.7.0及其以下版本的时候,我们可以直 ...
- Cannot instantiate the type ......的解决
使用public abstract class MainWindow implements ActionListener{} 之后创建对象MainWindow window = new MainWin ...
- httpclient获取响应实体和信息的封装方法(解耦更新)
转自:https://blog.csdn.net/fhaohaizi/article/details/77850302 2018年07月19日更新,主要是解耦之后方法很多地方发生了变化,httpcli ...
- 【教程】模拟登陆百度之Java代码版
[背景] 之前已经写了教程,分析模拟登陆百度的逻辑: [教程]手把手教你如何利用工具(IE9的F12)去分析模拟登陆网站(百度首页)的内部逻辑过程 然后又去用不同的语言: Python的: [教程]模 ...
随机推荐
- LeetCode(4) - Median of Two Sorted Arrays
题目要求很简单,就是给你两个已经排好序的数组nums1(长度为m)和nums2(长度为n),找出他们的中间值.返回值类型double是因为如果数字个数是偶数个,就要返回中间两个数的平均值.这题最简单的 ...
- NServiceBus-容器
NServiceBus自动注册以及用户实现其所有组件处理程序和传奇,这样所有实例化模式和连接在默认情况下都是正确的,没有错误. NServiceBus在容器构建(目前Autofac的ilmerge版本 ...
- js_sl 延迟菜单
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- dom 表格操作
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- ControlsFX8.0.2中对话框无法判断是否显示的修改
在org.controlsfx.dialog.FXDialog.java中加入 public abstract boolean isShowing(); 在org.controlsfx.dialog. ...
- HDU 1525 Euclid's Game (博弈)
Euclid's Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- Scriptcase优惠活动即将结束
前段时间我们开展了一段时间的Scriptcase打折优惠活动,现该活动即将结束,敬请知悉. Scriptcase是最好的PHP代码生成器,可以方便的与MySQL.Oracle.MSSQL.DB2等几乎 ...
- flash 定义主舞台窗口大小
1:[SWF(width=100 height=100)] 写在主类上面2:设置stageScaleMode属性为false;
- android自定义相册 支持低端机不内存溢出
1 之前在网上看的自定义相册很多时候在低端机都会内存溢出开始上代码把 首先我们要拿到图片的所有路径 cursor = context.getContentResolver().query( Media ...
- 快速找到跟踪其他session产生的trc文件
掌握该技术最根本的是需要搞清楚session跟踪文件存放的路径和生成跟踪文件的命名规则,不然,在已经存在成全上万trc文件的生产环境中,要想快速正确的找到跟踪其他SESSION产生的trc文件就如大海 ...