如何将 SOCKS 与 HtmlUnit 一起使用?
是否可以通过 SOCKS 代理使用 HtmlUnit?有人可以提供代码示例吗?
====
所以我已经挖掘了 webclient 源代码,这是我能想到的最好方法:
子类
MultiThreadedHttpConnectionManager
以便它允许设置 SOCKS 信息,如果设置了,则在返回之前连接,设置 SOCKS 参数子类
WebConnection
- 重写createHttpClient
,以便它使用步骤 1 中的管理器,并添加一个方法来直接获取该管理器或首先获取该管理器(现在它已受到保护 - 太糟糕了。 ..)使用 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:
Subclass
MultiThreadedHttpConnectionManager
so that it allows setting SOCKS info and if it is set, before returning a Connection, sets SOCKS parametersSubclass
WebConnection
- rewritecreateHttpClient
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...)To use 1) create a
WebClient
instance 2) Create subclassedWebConnection
3) Set it to be used byWebClient
4) Access connection's manager and use it's methods to use socks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要做的就是设置适当的系统属性< /a> 创建
WebClient
对象之前。例如:此时,HttpClient(HtmlUnit 在幕后使用)将获取设置并使用 SOCKS 代理进行所有网络通信。
更新:我阅读了您修改后的问题(以及您的评论),并且我认为您的方向是正确的。问题是,如果您使用上述系统属性实现步骤 1,那么您的代码就不是线程安全的(因为这些系统属性是全局的)。一种解决方案是同步某些内容,但这当然会带来性能问题(可能对您来说并不重要)。
如果您真的想在每个套接字的基础上控制它,那么我认为您需要执行如下操作:
的自定义
对象到ProtocolSocketFactory
>java.net.ProxySocket
构造函数(如 这个例子)。ProtocolSocketFactory
的自定义Protocol
。HttpConnection.setProtocol()
将此协议
应用到自定义连接管理器中的新连接。我还没有实际测试过这一点,但根据对 HttpClient 3.1 源代码的快速浏览,我认为这就是它的实现方式。我很想听听您最终如何解决这个问题:-)。祝你好运!
All you need to do is set the appropriate system properties before creating your
WebClient
object. For example: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:
ProtocolSocketFactory
that passes ajava.net.Proxy
object to theSocket
constructor (like in this example).Protocol
that uses thisProtocolSocketFactory
.Protocol
to the new connections in your custom connection manager usingHttpConnection.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!
HtmlUnit 使用 HttpClient 作为底层连接库,我对此进行了一些调查,但是:
HtmlUnit uses HttpClient as the underlying connection library, I investigated this a little, but: