0

我已经使用 htmlunit 一段时间了。现在我正在尝试向它引入超时功能。
它的工作方式如下:

WebClient webClient = new WebClient(BrowserVersion.INTERNET_EXPLORER_8);
//To disable throwing exception for script errors
webClient.setThrowExceptionOnScriptError(false);
//To disable java scripts
webClient.setJavaScriptEnabled(false);
//To disable the loading of CSS
webClient.setCssEnabled(false);
int timeout = 300;
// Add timeout for ETA
webClient.setTimeout(timeout);
try{
    webClient.setUseInsecureSSL(true);
}catch(GeneralSecurityException e){
    log.info("General Security Exception occured");
    log.error("",e);
}
HtmlPage loginPage = (HtmlPage) webClient.getPage("myurl");

如果我减少超时,它会给我一个ConnectionTimeOutException. 但是现在如果我更改设置顺序setTimeout并且setUseInsecureSSL它不起作用。我想说的是下面的代码不会让我连接超时。

WebClient webClient = new WebClient(BrowserVersion.INTERNET_EXPLORER_8);
//To disable throwing exception for script errors
webClient.setThrowExceptionOnScriptError(false);
//To disable java scripts
webClient.setJavaScriptEnabled(false);
//To disable the loading of CSS
webClient.setCssEnabled(false);
try{
    webClient.setUseInsecureSSL(true);
}catch(GeneralSecurityException e){
    log.info("General Security Exception occured");
    log.error("",e);
} 
//Setting it after setting "setUseInsecureSSL"
int timeout = 300;
// Add timeout for ETA
webClient.setTimeout(timeout);

谁能解释一下这是什么原因??
设置属性的顺序重要吗?

4

2 回答 2

2

有了这些问题,人们应该始终指定他们正在使用哪个版本的软件,在这种情况下是库,否则这个问题可能变得毫无意义......而不是浪费时间来谴责那些臭名昭著的人(“以个人经验为后盾”)蜥蜴、龙或任何他的名字都应该强制执行(“提供详细信息”)。话虽如此,我确认 HTMLUnit 2.7 到 2.9 至少具有如上所示的代码安排,因为第二个非工作示例不会引发任何与连接超时相关的异常(“回答问题”)。现在,我将使用 HTMLUnit 在我们的大型程序中反转有问题的 2 条候选行,并就此回复您(“分享您的研究”),但是,尽管如此,该图书馆深处的某个人对为什么会在地球上发生这种现象的解释将不胜感激!增强现实

于 2013-04-11T20:02:51.123 回答
2

我很惊讶你试图在这个地方使用 try-catch,因为你的 try-catch 永远不会工作。

您只是在初始化WebClient和设置功能。删除 try-catch 语句并在调用时使用它们webClient.getPage(...)

如果您针对一些非静态页面,300 毫秒也非常短。根据您的需要更改以下内容,并且在调用之前不要使用 try-catch webClient.getPage(...)

WebClient webClient = new WebClient();
webClient.getOptions().setUseInsecureSSL(true);
webClient.getOptions().setRedirectEnabled(true);
webClient.getOptions().setJavaScriptEnabled(false);
webClient.getOptions().setCssEnabled(false);
webClient.getOptions().setThrowExceptionOnScriptError(false);
webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
webClient.getCookieManager().setCookiesEnabled(false);
webClient.getOptions().setTimeout(8000);
webClient.getOptions().setDownloadImages(false);
webClient.getOptions().setGeolocationEnabled(false);
webClient.getOptions().setAppletEnabled(false);
于 2018-04-13T19:19:16.893 回答