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

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

问题描述

我有一个应用程序需要向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));

如果其他子系统可能希望通过其他方式访问Web服务器,这些都不是明智的选择不同的HTTP代理或没有任何代理。使用 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一样,需要一个 java.net.Proxy 对象。 Socket(java.net.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.

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

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