问题描述
我想为我的Jersey客户端配置代理服务器。
我不想将代理配置到整个应用程序(使用JVM参数,如http.proxyHost)和Id'而不是使用Apache客户端。
我读过有一个选项可以通过HttpUrlConnectionFactory提供HttpUrlConnection
,但我找不到任何代码示例。
有谁知道我怎么能做到了吗?
谢谢!
I would like to configure a proxy server to my Jersey client.
I don't want to configure the proxy to the whole application (using JVM arguments such as http.proxyHost), and Id'e rather not use Apache client.
I read here that there is an option to do it by providing HttpUrlConnectionvia HttpUrlConnectionFactory, but I couldn't find any code example.
Does anyone know how can I do it?
Thanks!
推荐答案
在Luca的帮助下,我完成了它:
With the help of Luca, I got it done:
-
实施
HttpURLConnectionFactory
,并覆盖方法getHttpURLConnection
,我的实现是(感谢Luca):
Implement
HttpURLConnectionFactory
, and override the methodgetHttpURLConnection
, my implementation is (thanks to Luca):
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 3128));
return new HttpURLConnection(url, proxy);
在实例化Jersey客户端之前,创建一个新的 URLConnectionClientHandler
,并在其构造函数中提供 HttpURLConnectionFactory
。然后创建一个新的客户端
,并在客户端$ c $中提供
ClientHandler
c>构造函数。我的代码:
Before instantiating the Jersey Client, create a new URLConnectionClientHandler
, and provide your HttpURLConnectionFactory
in its constructor. Then create a new Client
, and provide your ClientHandler
in the Client
constructor. My code:
URLConnectionClientHandler urlConnectionClientHandler = new URLConnectionClientHandler(new MyHttpURLConnectionFactory());
_client = new Client(urlConnectionClientHandler);
希望有帮助。
这篇关于配置代理到Jersey客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!