4

I'm trying to use HtmlUnit behind a proxy :

public class App {
public static void main(String[] args) throws Exception {

    System.setProperty("http.proxyHost", "172.23.232.10");
    System.setProperty("http.proxyPort", "8080");

    final WebClient webClient = new WebClient();
    final HtmlPage page = webClient.getPage("http://www.google.com");
    System.out.println(page.getTitleText());

     }
}

And I had a connection Time out exception

Exception in thread "main" org.apache.http.conn.HttpHostConnectException: Connect to www.google.com:80 [www.google.com/172.217.16.196] failed: Connection timed out: connect
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:158)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353)

Is there a solution to specify the proxy properties for HtmlUnit (browser)

K.Ariche
  • 1,188
  • 3
  • 14
  • 29

2 Answers2

3

Try this:

ProxyConfig proxyConfig = new ProxyConfig("172.23.232.10", 8080);
webClient.getOptions().setProxyConfig(proxyConfig);

You have more information here.

haihui
  • 1,075
  • 1
  • 18
  • 25
2

you can do proxy like this:

WebClient webClient = new WebClient(BrowserVersion.FIREFOX_45, proxyHost, proxyPort);
Kruti Patel
  • 1,422
  • 2
  • 23
  • 36
J.hero
  • 21
  • 2