我试图在我的应用程序中设置代理。当我尝试将其设置为系统属性时:
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()));
当我尝试连接到URL时,它会抛出java.net.ConnectException: Connection timed out: connect
:
URL url = new URL(urlToConnect);
HttpsURLConnection httpsConnection = (HttpsURLConnection) url.openConnection(); // Exception thrown in this line
但是,如果我将代理设置为openConnection()
的参数:
HttpsURLConnection httpsConnection = (HttpsURLConnection) url.openConnection(proxy);
我的代码工作正常,我可以连接到网址,但是这个解决方案是不切实际的,因为我的代码中有很多openConnection()
。
如何在将其用作系统属性时使其正常工作?
答案 0 :(得分:5)
我尝试访问的网址是https,我正在设置http.proxyHost
和http.proxyPort
。将其更改为https.proxyHost
和https.proxyHost
,它有效