我正在编写一个独立的 Java 应用程序。我必须使用默认系统(Windows XP、7)代理设置来调用使用 HTTPS 协议的 SOAP Web 服务。
要确定系统代理设置,我使用 ProxySelector 类。这是代码:
System.setProperty("java.net.useSystemProxies", "true");
List proxies = ProxySelector.getDefault().select(new URI("https://www.google.com/"));
for (Iterator iter = proxies.iterator(); iter.hasNext();) {
Proxy proxy = (Proxy) iter.next();
InetSocketAddress address = (InetSocketAddress) proxy.address();
if (address == null) {
UTLogger.getLogger().debug("No proxy (direct connection)");
} else {
if (proxy.type().equals(Proxy.Type.HTTP)) {
System.setProperty("http.proxyHost", address.getHostName());
System.setProperty("http.proxyPort", String.valueOf(address.getPort()));
System.setProperty("https.proxyHost", address.getHostName());
System.setProperty("https.proxyPort", String.valueOf(address.getPort()));
} else if (proxy.type().equals(Proxy.Type.SOCKS)) {
System.setProperty("socksProxyHost", address.getHostName());
System.setProperty("socksProxyPort", String.valueOf(address.getPort()));
}
}
}
System.setProperty("java.net.useSystemProxies", "false");
这是我初始化连接的方式:
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, new TrustManager[]{new TrustDummy()}, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
servicePort = new MyPort(new URL(wsdlLocation), new QName(nameSpaceURI, localPart)).getMyPortSoap();
我不明白为什么没有Proxy.Type.HTTPS
?如果我调用 Web 服务方法,我会收到以下错误。
使用 http 代理集:
javax.xml.ws.WebServiceException: Failed to access the WSDL at: ht tps://...?wsdl. It failed with: Got Unrecognized SSL message, plaintext connection? while opening stream from ht tps://...?wsdl.
并使用 https 代理集:
javax.xml.ws.WebServiceException: Failed to access the WSDL at: ht tps://...?wsdl. It failed with: Got Unable to tunnel through proxy. Proxy returns "HTTP/1.0 403 Forbidden" while opening stream from ht tps://...?wsdl.
我做错了吗?