1

I'm trying to set a proxy to use in my application. When I try to set it as a system property:

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()));

it throws java.net.ConnectException: Connection timed out: connect when I try to connect to a URL:

URL url = new URL(urlToConnect);
HttpsURLConnection httpsConnection = (HttpsURLConnection) url.openConnection(); // Exception thrown in this line

But, if I set the proxy as a parameter to openConnection():

HttpsURLConnection httpsConnection = (HttpsURLConnection) url.openConnection(proxy); 

my code works and I am able to connect to URL, but this solution is impracticable since I have many openConnection() in my code.

How can I make it work when using it as system properties?

lucasdc
  • 1,032
  • 2
  • 20
  • 42
  • did you try to establish the connection from browser with same url that is throwing exception ? – M Sach Jul 08 '16 at 16:20
  • yes.. it works ok @MSach – lucasdc Jul 08 '16 at 16:21
  • is the url http or https ? – M Sach Jul 08 '16 at 16:22
  • According to the [docs](https://docs.oracle.com/javase/7/docs/api/java/net/doc-files/net-properties.html): _On recent Windows systems and on Gnome 2.x systems it is possible to tell the java.net stack, setting this property to true, to use the system proxy settings (both these systems let you set proxies globally through their user interface). Note that this property is checked only once at startup._ Are you on Windows or Gnome 2.x? If not, sounds like you can't use the property. If you are, then set the property on the command line, not within your code. – Chuck Daniels Jul 09 '16 at 01:52

1 Answers1

5

The URL I was trying to access was https and I was setting http.proxyHost and http.proxyPort. Changing it to https.proxyHost and https.proxyHost, it worked

lucasdc
  • 1,032
  • 2
  • 20
  • 42