如何在不设置系统范围属性的情况下为 JAX-WS 请求使用 HTTP 代理? [英] How can I use an HTTP proxy for a JAX-WS request without setting a system-wide property?

查看:36
本文介绍了如何在不设置系统范围属性的情况下为 JAX-WS 请求使用 HTTP 代理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序需要向 Internet 上的系统发出 SOAP 客户端请求,因此它需要通过我们的 HTTP 代理.

I have an application that needs to make a SOAP client request to a system on the Internet, so it needs to go though our HTTP proxy.

可以通过设置系统范围的值(例如系统属性)来实现这一点:

One can do this by setting system-wide values such as system properties:

// Cowboy-style.  Blow away anything any other part of the application has set.
System.getProperties().put("proxySet", "true");
System.getProperties().put("https.proxyHost", HTTPS_PROXY_HOST);  
System.getProperties().put("https.proxyPort", HTTPS_PROXY_PORT);

或者通过设置默认的 ProxySelector(也是系统范围的设置):

Or by setting the default ProxySelector (also a system-wide setting):

// More Cowboy-style!  Every thing Google has found says to do it this way!?!?!
ProxySelector.setDefault(new MyProxySelector(HTTPS_PROXY_HOST, HTTPS_PROXY_PORT));

如果其他子系统可能希望通过不同的 HTTP 代理或没有任何代理访问 Web 服务器,那么这两种方法都不是明智的选择.使用 ProxySelector 可以让我配置哪些连接使用代理,但我必须为庞大的应用程序中的每一件事都弄清楚这一点.

Neither of these is a wise choice if there is the possibility of other subsystems wanting to access web servers via different HTTP proxies or without any proxy. Using the ProxySelector would let me configure which connections use the proxy, but I would have to figure that out for every single thing in the huge application.

一个合理的 API 应该有一个方法,它接受一个 java.net.Proxy 对象,就像 java.net.Socket(java.net.Proxy proxy) 构造函数一样做.这样,必要的设置对于需要设置它们的系统部分来说是本地的.有没有办法用 JAX-WS 做到这一点?

A reasonable API would have a method that took a java.net.Proxy object just like the java.net.Socket(java.net.Proxy proxy) constructor does. That way the necessary settings are local to the part of the system that needs to set them. Is there some way to do this with a JAX-WS?

我不想设置系统范围的代理配置.

I do not want to set a system-wide proxy configuration.

推荐答案

如果您使用 JAX-WS,您或许能够设置底层 HttpURLConnection 使用的套接字工厂.我看到模糊的迹象表明 SSL 是可行的(参见 HTTPS SSLSocketFactory),但我不确定您是否可以为常规 HTTP 连接执行此操作(或者坦率地说它是如何工作的:他们引用的 JAXWSProperties 类似乎是一个非标准的 JDK 类).

If you are using JAX-WS you might be able to set the socket factory used by the underlying HttpURLConnection. I see vague signs that this is possible for SSL (see HTTPS SSLSocketFactory) but I'm not certain if you can do that for regular HTTP connections (or quite frankly how that even works: the JAXWSProperties class they reference appears to be a non-standard JDK class).

如果您可以设置套接字工厂,那么您可以配置一个使用您想要的特定代理的自定义套接字工厂.

If you can set the socket factory then you can configure a custom socket factory that uses the specific proxy you want.

这篇关于如何在不设置系统范围属性的情况下为 JAX-WS 请求使用 HTTP 代理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆