如何将 SOCKS 与 HtmlUnit 一起使用?

发布于 2024-08-24 04:20:22 字数 519 浏览 6 评论 0原文

是否可以通过 SOCKS 代理使用 HtmlUnit?有人可以提供代码示例吗?

====

所以我已经挖掘了 webclient 源代码,这是我能想到的最好方法:

  1. 子类 MultiThreadedHttpConnectionManager 以便它允许设置 SOCKS 信息,如果设置了,则在返回之前连接,设置 SOCKS 参数

  2. 子类 WebConnection - 重写 createHttpClient ,以便它使用步骤 1 中的管理器,并添加一个方法来直接获取该管理器或首先获取该管理器(现在它已受到保护 - 太糟糕了。 ..)

  3. 使用 1) 创建一个 WebClient 实例 2) 创建子类 WebConnection 3)将其设置为由 WebClient 使用 4) 访问连接管理器并使用其方法来使用袜子

Is it possible to use HtmlUnit through SOCKS proxy? Could anyone please provide a code sample?

====

So I've dug through webclient sources, here's the best way I can think of:

  1. Subclass MultiThreadedHttpConnectionManager so that it allows setting SOCKS info and if it is set, before returning a Connection, sets SOCKS parameters

  2. Subclass WebConnection - rewrite createHttpClient so that it uses a manager from step 1 and add a method to get that manager directly or http client at first (it is protected now - so bad...)

  3. To use 1) create a WebClient instance 2) Create subclassed WebConnection 3) Set it to be used by WebClient 4) Access connection's manager and use it's methods to use socks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

So要识趣 2024-08-31 04:20:22

您需要做的就是设置适当的系统属性< /a> 创建 WebClient 对象之前。例如:

System.setProperty("socksProxyHost", "localhost"); // replace "localhost" with your proxy server
System.setProperty("socksProxyPort", "9999"); // replace "9999" with your proxy port number

WebClient client = new WebClient();

此时,HttpClient(HtmlUnit 在幕后使用)将获取设置并使用 SOCKS 代理进行所有网络通信。

更新:我阅读了您修改后的问题(以及您的评论),并且我认为您的方向是正确的。问题是,如果您使用上述系统属性实现步骤 1,那么您的代码就不是线程安全的(因为这些系统属性是全局的)。一种解决方案是同步某些内容,但这当然会带来性能问题(可能对您来说并不重要)。

如果您真的想在每个套接字的基础上控制它,那么我认为您需要执行如下操作:

  1. 创建一个传递的自定义ProtocolSocketFactory >java.net.Proxy 对象到 Socket 构造函数(如 这个例子)。
  2. 创建使用此 ProtocolSocketFactory 的自定义 Protocol
  3. 使用 HttpConnection.setProtocol() 将此协议应用到自定义连接管理器中的新连接。

我还没有实际测试过这一点,但根据对 HttpClient 3.1 源代码的快速浏览,我认为这就是它的实现方式。我很想听听您最终如何解决这个问题:-)。祝你好运!

All you need to do is set the appropriate system properties before creating your WebClient object. For example:

System.setProperty("socksProxyHost", "localhost"); // replace "localhost" with your proxy server
System.setProperty("socksProxyPort", "9999"); // replace "9999" with your proxy port number

WebClient client = new WebClient();

At this point, HttpClient (which is used by HtmlUnit under the covers) will pick up the settings and use the SOCKS proxy for all network communication.

UPDATE: I read your revised question (and your comment) and I think you're on the right track. The problem is that if you implement step 1 using the above system properties, then your code is not thread-safe (because those system properties are global). One solution is to synchronize on something, but of course this can introduce performance problems (may not matter to you).

If you really want to control this in a per-socket basis, then I think you will need to do something like the following:

  1. Create a custom ProtocolSocketFactory that passes a java.net.Proxy object to the Socket constructor (like in this example).
  2. Create a custom Protocol that uses this ProtocolSocketFactory.
  3. Apply this Protocol to the new connections in your custom connection manager using HttpConnection.setProtocol().

I haven't actually tested this, but based on a quick glance at the HttpClient 3.1 source code, I think that's how it would be done. I would love to hear how you ultimately solve this problem :-). Good luck!

十六岁半 2024-08-31 04:20:22

HtmlUnit 使用 HttpClient 作为底层连接库,我对此进行了一些调查,但是:

1- Couldn't find a way to configure HttpClient (except by the generic Java Socks mechanism defined in http://java.sun.com/javase/6/docs/technotes/guides/net/proxies.html)
2- Do not have access to a public Socks Proxy to test against

HtmlUnit uses HttpClient as the underlying connection library, I investigated this a little, but:

1- Couldn't find a way to configure HttpClient (except by the generic Java Socks mechanism defined in http://java.sun.com/javase/6/docs/technotes/guides/net/proxies.html)
2- Do not have access to a public Socks Proxy to test against
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文