java爬虫 报302_java爬虫:httpClient发起请求时报ClientProtocolException的错误

背景:

java爬虫抓取某保险网站的数据。

问题:

在模拟登录之后,对目标页面发起请求时报错。

登陆成功。。。。。

org.apache.http.client.ClientProtocolException

at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:186)

at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)

at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:107)

at spider.SimulatePALogin.afterLoginPager(SimulatePALogin.java:92)

at spider.SimulatePALogin.main(SimulatePALogin.java:41)

Caused by: org.apache.http.ProtocolException: Content-Length header already present

at org.apache.http.protocol.RequestContent.process(RequestContent.java:96)

at org.apache.http.protocol.ImmutableHttpProcessor.process(ImmutableHttpProcessor.java:132)

at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:182)

at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)

at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)

at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)

... 4 more

Process finished with exit code 0

出错代码位置:

HttpUriRequest httpUriRequest = new HttpPost(

"https://icore-pts.pingan.com.cn/ebusiness/login");

// ?Accept?: application/json, text/javascript, */*; q=0.01

httpUriRequest.setHeader("Host","pacas-login.pingan.com.cn");

//...... set headers here

httpUriRequest.setHeader("Upgrade-Insecure-Requests","1");

httpUriRequest

.setHeader(

"User-Agent",

"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.89 Safari/537.36");

// 报错位置

HttpResponse response = HttpUtils.httpClient.execute(httpUriRequest);

初接触爬虫,请问各位大大为什么会出错?求指教!!

先在此谢过。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用Java HttpClient发起请求,你可以按照以下步骤进行操作: 1. 首先,创建一个HttpPost对象,将请求的URL作为参数传递给它。可以使用引用中的示例代码中的以下行完成此步骤: ``` HttpPost httpPost = new HttpPost(url); ``` 2. 接下来,创建一个CloseableHttpClient对象,可以使用引用中的示例代码中的以下行完成此步骤: ``` CloseableHttpClient httpClient = HttpClientBuilder.create().build(); ``` 3. 设置请求的数据格式和内容。你可以使用StringEntity类创建一个包含请求数据的实体。可以使用引用中的示例代码中的以下行完成此步骤: ``` StringEntity entity = new StringEntity(jsonData, "utf-8"); entity.setContentEncoding("UTF-8"); entity.setContentType("application/json"); httpPost.setEntity(entity); ``` 4. 执行POST请求并获取响应结果。可以使用httpClient的execute方法来执行请求,并使用HttpResponse对象接收响应结果。你可以根据需要使用不同的方法来处理响应结果。例如,可以使用BasicResponseHandler类的实例来处理响应结果字符串。可以使用引用中的示例代码中的以下行完成此步骤: ``` BasicResponseHandler handler = new BasicResponseHandler(); result = httpClient.execute(httpPost, handler); ``` 5. 最后,记得释放连接。可以使用httpClient的close方法来关闭连接。可以使用引用中的示例代码中的以下行完成此步骤: ``` httpClient.close(); ``` 综上所述,以上步骤提供了一个示例的Java HttpClient发起请求的过程。根据你的需求,你还可以根据具体的情况进行适当的调整和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值