How to call getPage from HtmlUnit WebClient and disable setTimeout?

I have the same problem as described in the question Calling getPage from htmlunit WebClient with JavaScript disabled and setTimeout set to 10000, is waiting forever .

There is only one important (complex) possible answer ( by themtoo ). Therefore, I was wondering if:

  • Does anyone have a simpler answer?
  • Can someone check if the solution works?
+1
settimeout htmlunit
source share
1 answer

The code I used:

package main; import java.io.IOException; import java.net.MalformedURLException; import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException; import com.gargoylesoftware.htmlunit.WebClient; public class Test { public static void main(final String[] args) { final WebClient webClient = new WebClient(); webClient.setTimeout(1000); try { System.out.println("Querying"); webClient.getPage("http://www.google.com"); System.out.println("Success"); } catch (final FailingHttpStatusCodeException e) { System.out.println("One"); e.printStackTrace(); } catch (final MalformedURLException e) { System.out.println("Two"); e.printStackTrace(); } catch (final IOException e) { System.out.println("Three"); e.printStackTrace(); } catch (final Exception e) { System.out.println("Four"); e.printStackTrace(); } System.out.println("Finished"); } } 

Output (removed all CSS and JS warnings):

 Querying Success Finished 

After changing the timeout from 1000 to 1 (I will not delete Google in less than 1 ms):

 Querying Three org.apache.http.conn.ConnectTimeoutException: Connect to www.google.com:80 timed out at com.gargoylesoftware.htmlunit.SocksSocketFactory.connectSocket(SocksSocketFactory.java:92) at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:148) at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:149) at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:121) at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:573) at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:425) at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:820) at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:776) at com.gargoylesoftware.htmlunit.HttpWebConnection.getResponse(HttpWebConnection.java:152) at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseFromWebConnection(WebClient.java:1439) at com.gargoylesoftware.htmlunit.WebClient.loadWebResponse(WebClient.java:1358) at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:307) at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:373) at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:358) at main.Test.main(Test.java:17) Finished 

Conclusion: I can not reproduce it, and it works as expected

+3
source share

All Articles