我正在尝试设置代理以在我的应用程序中使用.当我尝试将其设置为系统属性时:
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()));
Run Code Online (Sandbox Code Playgroud)
java.net.ConnectException: Connection timed out: connect当我尝试连接到URL时它会抛出:
URL url = new URL(urlToConnect);
HttpsURLConnection httpsConnection = (HttpsURLConnection) url.openConnection(); // Exception thrown in this line
Run Code Online (Sandbox Code Playgroud)
但是,如果我将代理设置为参数openConnection():
HttpsURLConnection httpsConnection = (HttpsURLConnection) url.openConnection(proxy);
Run Code Online (Sandbox Code Playgroud)
我的代码工作,我能够连接到URL,但这个解决方案是不切实际的,因为openConnection()我的代码中有很多.
使用它作为系统属性时,如何使其工作?
我试图访问的URL是https,我正在设置http.proxyHost和http.proxyPort.改变它,https.proxyHost并且https.proxyHost,它工作
| 归档时间: |
|
| 查看次数: |
1934 次 |
| 最近记录: |