我正在尝试设置要在我的应用程序中使用的代理。当我尝试将其设置为系统属性时:
Proxy proxy = ... // code to retrieve proxy from .pac file
InetSocketAddress addr = (InetSocketAddress) proxy.address();
System.setProperty("java.net.useSystemProxies", "true");
System.setProperty("http.proxyHost", addr.getHostName());
System.setProperty("http.proxyPort", Integer.toString(addr.getPort()));
java.net.ConnectException: Connection timed out: connect
当我尝试连接到URL时会抛出该错误:
URL url = new URL(urlToConnect);
HttpsURLConnection httpsConnection = (HttpsURLConnection) url.openConnection(); // Exception thrown in this line
但是,如果我将proxy设置为参数openConnection()
:
HttpsURLConnection httpsConnection = (HttpsURLConnection) url.openConnection(proxy);
我的代码可以工作,并且可以连接到URL,但是由于openConnection()
我的代码很多,因此该解决方案不可行。
将其用作系统属性时,如何使它工作?
我尝试访问的URL是https,并且我设置了http.proxyHost
和http.proxyPort
。将其更改为https.proxyHost
和https.proxyHost
,它有效
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句