在 HtmlUnit 中设置代理
我在 htmlunit 中使用代理,我的代理列表包含 http 和ocks 的混合,我不知道下一个要传递给 htmlunit 的选定代理是否是 http 类型或袜子类型,htmlunit 会自动确定类型并使用适当的规则通过该代理连接?
I'm using proxies with htmlunit, my proxy list contain mixture of both http and socks, I dont know if the next selected proxy to be passed to htmlunit is type http or socks, will htmlunit automatically determine the type and use the appropriate rule for connecting through that proxy?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
查看 Java 网络和代理。
它讨论了设置代理的多种策略。它还提供了通过代理选择器向同一连接提供多个代理的选项。
Check out Java Networking and Proxies.
It talks about multiple strategies for setting proxies. It also gives option to provide multipe proxy to same connection through proxy selector.
另外两个答案是关于如何在 Java 程序中一般使用代理,但与 HtmlUnit 有点不同。在 Java 进程上设置代理不会执行任何操作;相反,您希望模拟浏览器使用代理。
The other two answers are about how to generically use a proxy in a Java program, but it is a little bit different with HtmlUnit. Setting the proxy on the Java process does nothing; instead you want the simulated browser to use the proxy.
该框架会为您检测代理类型,并可以使用正确的 Socks/Http/Https 代理配置为您实例化 HtmlUnit WebClient: https://github.com/subes/invesdwin-webproxy
本质上,它在为您维护代理列表时会自动执行试错方法。因此,您不必在自己的代码中担心这一点。
This framework detects the proxy type for you and can instantiate a HtmlUnit WebClient with correct Socks/Http/Https proxy configuration for you: https://github.com/subes/invesdwin-webproxy
Essentially it automates the trial&error approach when maintaining its proxy list for you. So you don't have to worry about that in your own code.
我制作了一个也使用代理混合的应用程序,但那是不久前的事了。在该版本的 HtmlUnit 中,需要明确告知代理是否是 SOCKS,否则它假定它是 HTTP 代理。我简单地查看了更改日志,没有发现任何表明这已更改的信息,因此答案应该是否定的,如果您不告诉 HtmlUnit 代理是 SOCKS,它将假定代理是 HTTP。
要检查代理是什么类型,可以使用以下内容:
如果代码失败(即抛出异常),则代理很可能已失效或 SOCKS。 (无论如何,HtmlUnit 在第一种情况下都会抛出异常,或者如果您不确定代理是否处于活动状态,您可以使用 Proxy.Type.SOCKS 再次执行相同的测试。)
I've made an application which used mixture of proxies as well, but it was a while ago. In that version of HtmlUnit, it required being explicitly told if the proxy was SOCKS or not, otherwise it assumed it was a HTTP proxy. I looked briefly in the change logs, and found nothing indicating that this had changed, so the answer should be no, it will assume that the proxy is HTTP if you don't tell HtmlUnit that the proxy is SOCKS.
To check what type a proxy is, one can use something like:
If the code fails (i.e throws an exception), then the proxy is most likely either dead or SOCKS. (HtmlUnit will throw an exception in the first case anyway, or you can perform the same test again with Proxy.Type.SOCKS if you aren't certain the proxy is alive.)