如何在HtmlUnit中使用socks

时间:2011-12-12 04:24:48

标签: java exception htmlunit socks

我用谷歌搜索了三个小时,没有发现任何帮助。 我有通过socks 5连接到某些网站的WebClient,不需要使用登录名和密码来处理它们:

WebClient wc = new WebClient(BrowserVersion.FIREFOX_3_6);
        wc.setProxyConfig(new ProxyConfig(socks.split(":")[0], Integer.parseInt(socks.split(":")[1]), true));
        wc.setTimeout(30000);
        wc.setCssEnabled(false);
        wc.setJavaScriptEnabled(false);

等等。我收到了不需要登录或密码的EVERYONE alive主机的下一个例外:

java.net.SocketException: SOCKS : authentication failed
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:473)
    at java.net.Socket.connect(Socket.java:579)
    at com.gargoylesoftware.htmlunit.SocksSocketFactory.connectSocket(SocksSocketFactory.java:89)
    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 mus.Gucol.run(Gucol.java:49)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)

Dec 12, 2011 4:14:02 AM org.apache.http.impl.client.DefaultRequestDirector tryConnect
INFO: I/O exception (java.net.SocketException) caught when connecting to the target host: SOCKS : authentication failed

HtmlUnit 2.9。我试过使用凭证:

WebClient wc = new WebClient(BrowserVersion.FIREFOX_3_6);
        wc.setProxyConfig(new ProxyConfig(socks.split(":")[0], Integer.parseInt(socks.split(":")[1]), true));
        wc.setTimeout(30000);
        wc.setCssEnabled(false);
        wc.setJavaScriptEnabled(false);

        final DefaultCredentialsProvider credentialsProvider = (DefaultCredentialsProvider) wc.getCredentialsProvider();
        credentialsProvider.addCredentials("", "", socks.split(":")[0], Integer.parseInt(socks.split(":")[1]), null);
        credentialsProvider.addProxyCredentials("", "");

        wc.setCredentialsProvider(credentialsProvider);

但这没有任何意义。我认为问题不在HtmlUnit中,而是在Java中,因为我试图通过HtmlUrlConnection使用socks,并且发生了同样的错误,这让我哭了。伙计们,如何让它发挥作用?我花了大约六个小时来解决这个问题,我不能使用System.setProperty因为我需要为每个WebClient“窗口”使用不同的袜子。

1 个答案:

答案 0 :(得分:0)

您是否尝试使用空值而不是空字符串?

BTW,不推荐使用addProxyCredentials()