Configure proxy to Jersey client(为 Jersey 客户端配置代理)
问题描述
我想为我的 Jersey 客户端配置一个代理服务器.
我不想为整个应用程序配置代理(使用 JVM 参数,例如 http.proxyHost),我宁愿不使用 Apache 客户端.
我读到 这里 有一个选项通过提供 HttpUrlConnection 来做到这一点通过 HttpUrlConnectionFactory,但我找不到任何代码示例.
有谁知道我该怎么做?
谢谢!
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 HttpUrlConnection
via 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
.然后创建一个新的 Client
,并在 Client
构造函数中提供您的 ClientHandler
.我的代码:
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 客户端配置代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为 Jersey 客户端配置代理


基础教程推荐
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 降序排序:Java Map 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01