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:
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?