在代理后面使用 HtmlUnit

using HtmlUnit behind proxy

我正在尝试在代理后面使用 HtmlUnit :

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

     }
}

我有一个连接超时异常

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)

是否有为 HtmlUnit(浏览器)指定代理属性的解决方案

试试这个:

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

您有更多信息here

你可以这样proxy

WebClient webClient = new WebClient(BrowserVersion.FIREFOX_45, proxyHost, proxyPort);