Java htmlunit setTimeOut的工作方式很奇怪

Java htmlunit setTimeOut的工作方式很奇怪,java,timeout,htmlunit,Java,Timeout,Htmlunit,我已经用了一段时间了。现在,我正在尝试为它引入超时功能。 其工作方式如下: WebClient webClient = new WebClient(BrowserVersion.INTERNET_EXPLORER_8); //To disable throwing exception for script errors webClient.setThrowExceptionOnScriptError(false); //To disable java scripts webClient.setJ

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

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);
有人能解释一下原因吗??

设置属性的顺序重要吗?

有了这些问题,人们应该始终指定他们正在使用的软件、库的哪个版本,否则问题可能变得毫无意义。。。与其把时间浪费在谴责人们(“以个人经验为依据”)上,还不如让臭名昭著的蜥蜴、龙或任何他的名字来强制执行(“提供细节”)。话虽如此,我确认HTMLUnit 2.7到2.9至少在上面所示的第二个非工作示例的代码安排下不会抛出任何与连接超时相关的异常(“回答问题”)。现在,我将使用HTMLUnit反转我们的大型程序中有问题的两行候选行,并就此与您联系(“分享您的研究”),但尽管如此,我们还是希望该库深处的某个人能够解释为什么会在地球上发生这样的现象!A.R.

对于这些问题,人们应该始终指定他们正在使用的软件、库的版本,否则问题可能变得毫无意义。。。与其把时间浪费在谴责人们(“以个人经验为依据”)上,还不如让臭名昭著的蜥蜴、龙或任何他的名字来强制执行(“提供细节”)。话虽如此,我确认HTMLUnit 2.7到2.9至少在上面所示的第二个非工作示例的代码安排下不会抛出任何与连接超时相关的异常(“回答问题”)。现在,我将使用HTMLUnit反转我们的大型程序中有问题的两行候选行,并就此与您联系(“分享您的研究”),但尽管如此,我们还是希望该库深处的某个人能够解释为什么会在地球上发生这样的现象!A.R.

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

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

另外,如果您的目标是一些非静态页面,300毫秒是非常短的。根据需要更改以下内容,在调用
webClient.getPage(…)
之前不要使用try-catch


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

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

另外,如果您的目标是一些非静态页面,300毫秒是非常短的。根据需要更改以下内容,在调用
webClient.getPage(…)
之前不要使用try-catch

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);