2

Following on from my previous question.

When I go via a proxy server on my PC, I can't connect to the internet in Java unless the proxy is manually specified - either with no arguments specified, or with java.net.useSystemProxies. I would've expected java.net.useSystemProxies to use the system values, but it doesn't seem to.

Take the following simple application, that connects to AWS' Check IP service and prints the results to the console.

final URI uri = URI.create("https://checkip.amazonaws.com/");

System.out.println(MessageFormat.format("Proxy - {0}", ProxySelector.getDefault().select(uri)));

System.out.println(MessageFormat.format("IP - {0}", HttpClient.newHttpClient().send(HttpRequest.newBuilder(uri).build(), BodyHandlers.ofString()).body()));

When run, it prints the following:

Proxy - [DIRECT]
IP - {MY PUBLIC IP}

I then set my PC to use a proxy that I found on this website:

enter image description here

When I access AWS' Check IP service, the IP returned is now that of the proxy - 176.9.117.112.

Now when I re-run my application, without any arguments, it still connects - but not via the proxy:

Proxy - [DIRECT]
IP - {MY PUBLIC IP}

With the -D"java.net.useSystemProxies"=true argument set, the behaviour is the same - still not via the proxy:

Proxy - [DIRECT]
IP - {MY PUBLIC IP}

Finally, when I manually specify the proxy server via the arguments -Dhttp.proxyHost=176.9.117.112 -Dhttp.proxyPort=3128 -Dhttps.proxyHost=176.9.117.112 -Dhttps.proxyPort=3128, it's clear the traffic is going via the proxy:

Proxy - [HTTP @ 176.9.117.112/<unresolved>:3128]
IP - 176.9.117.112

So my question is - why doesn't java.net.useSystemProxies use system proxies?

Jakg
  • 922
  • 12
  • 39
  • `-D"java.net.useSystemProxies"=true` are qotes in this typo? Did you try `-Djava.net.useSystemProxies=true` Did you trye setting `System.setProperty("java.net.useSystemProxies", "true");` in code? – Bartek Jablonski Nov 19 '21 at 14:06
  • @BartekJablonski ``-D"java.net.useSystemProxies"=true`` vs ``-Djava.net.useSystemProxies=true`` both return the same results. ``System.setProperty("java.net.useSystemProxies", "true");`` gives the same result as well. No proxies used for any. – Jakg Nov 19 '21 at 14:34
  • What version of Java is this? – Codebling Nov 25 '21 at 17:53
  • 1
    @Codebling I can reproduce this on Java 11 & Java 15. I've not tried any others. – Jakg Nov 25 '21 at 21:03
  • You're not using a Security Manager, right? – Codebling Nov 25 '21 at 22:37
  • @Codebling not to my knowledge, no. – Jakg Nov 26 '21 at 07:28

0 Answers0