多次调用后htmlunit webclient收到403

时间:2019-03-05 19:23:49

标签: java web-scraping webclient http-status-code-403 htmlunit-driver

我正在使用以下htmlunit版本

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.141.59</version>
    </dependency>

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>htmlunit-driver</artifactId>
        <version>2.33.3</version>
    </dependency>

我的代码做什么

  1. 使用CHROME浏览器版本创建WebClient
  2. 获取网页并使用html表单执行登录
  3. 使用同一客户端获取其他网页并解析信息

如果我连续执行1到3次,则可以正常工作,但是此后,随后的任何尝试都将出现以下403错误。

mar 05, 2019 12:57:49 PM com.gargoylesoftware.htmlunit.WebClient printContentIfNecessary 
INFO: statusCode=[403] contentType=[application/json] 
mar 05, 2019 12:57:49 PM com.gargoylesoftware.htmlunit.WebClient printContentIfNecessary
INFO: {"error_description":"Access Denied","error_uri":"HTTP://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html","error":"Access Denied"}
mar 05, 2019 12:57:49 PM com.gargoylesoftware.htmlunit.WebClient printContentIfNecessary
INFO: statusCode=[403] contentType=[application/json] 
mar 05, 2019 12:57:49 PM com.gargoylesoftware.htmlunit.WebClient printContentIfNecessary
INFO: {"error_description":"Access Denied","error_uri":"HTTP://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html","error":"Access Denied"}
java.nio.file.AccessDeniedException: Access Denied

我尝试清除客户端的cookie并关闭客户端,然后再试一次,但它却得到了403。

但是,如果我停止执行程序并再次运行它,它将再次正常运行3次,此后开始显示403s。

在尝试与新客户端重试之前,我试图睡3分钟,看来可以正常完成重试,但是下一次尝试得到403,必须再次等待。

我的猜测是服务器拒绝多个会话的访问,但是我想知道是否有一种方法可以“清理”环境并重试,类似于停止执行并再次运行程序时发生的情况

由于清除cookieManager上的cookie无效,所以我不知道还能尝试模拟停止并再次运行的其他方式。

0 个答案:

没有答案