gpt4 book ai didi

java - 如何使用 HtmlUnit 处理太多重定向

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:43:41 26 4
gpt4 key购买 nike

我正在尝试解析一个站点,但遇到了太多重定向 异常。这是我的代码:

WebClient client = new WebClient(BrowserVersion.FIREFOX_24);
HtmlPage homePage = null;
String url = "http://www.freelake.org/pages/Freetown-Lakeville_RSD/Departments/Director_of_Financial_Operatio";
try {
client.getOptions().setUseInsecureSSL(true);
client.setAjaxController(new NicelyResynchronizingAjaxController());
client.getOptions().setThrowExceptionOnFailingStatusCode(false);
client.getOptions().setThrowExceptionOnScriptError(false);
client.waitForBackgroundJavaScript(30000);
client.waitForBackgroundJavaScriptStartingBefore(30000);
client.getOptions().setCssEnabled(false);
client.getOptions().setJavaScriptEnabled(true);
client.getOptions().setRedirectEnabled(true);
homePage = client.getPage(url);
synchronized (homePage) {
homePage.wait(25000);
}
System.out.println(homePage.asXml());
} catch (Exception e) {
e.printStackTrace();
}

下面提到的异常(exception)情况

com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException: Too much redirect for http://www.freelake.org/resolver/2345183424.20480.0000/route.00/pages/Freetown-Lakeville_RSD/Departments/Director_of_Financial_Operatio
at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseFromWebConnection(WebClient.java:1353)
at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseFromWebConnection(WebClient.java:1371)

有什么办法可以解决这个问题吗?

最佳答案

这是因为 HtmlUnit 缓存了响应,然后重定向到另一个页面然后返回。

我用下面的测试,它有效:

client.getCache().setMaxSize(0);

关于java - 如何使用 HtmlUnit 处理太多重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30370056/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com