Question

I use this code to get the system proxies in Java:

System.setProperty("java.net.useSystemProxies", "true");
    List<Proxy> list = null;
    try
    {
        list = ProxySelector.getDefault().select(new URI("http://google.com"));
    }
    catch (final URISyntaxException e)
    {
        e.printStackTrace();
    }

But "list" only contains DIRECT, but not the settings I set in Internet Explorer. The strange thing is, that in an earlier version of our software the exact same code returns the correct list of system proxies.

Does anyone know what could possibly make it impossible for the code to work properly?

I added some native libraries, that is the only thing I could think of. But still I have no idea how this could affect this code. I tested the same code in a little test app, within the same eclipse environment etc. and it works. But when I debug into the same code within my application the list only contains DIRECT.

Any ideas?

Was it helpful?

Solution

The native libraries were the problem. I used NativeSwing, to embed a WebBrowser in Swing. When doing NativeInterface.open() or NativeInterface.initialize() the proxy settings are changed and cannot be changed afterwards. I therefore now read the proxy settings and save them to a static field. Then when I need them I read them from this static field.

The story continues: The above approach only worked from within Eclipse. If I double clicked on the jar I got "The native side did not receive an incoming connection!". The reason for that is not because of the NativeInterface I'm using, but has something to do with how java handles system proxy settings.

I found this article: http://ideen2011.blogspot.de/2011/08/java-proxyselector-usesystemproxies-and.html

In short: Use ProxySelector.setDefault(null); if you want socket connections to be not affected by system proxies etc. At least that helped me, but I'm still trying to understand, what exactly is going on in the background.

Story continues in 2015: The previouly mentioned approach caused this issue: JavaFX webview set Proxy

So I recommend instead of

ProxySelector.setDefault(null)

to use

ProxySelector.setDefault(ProxySelector.getDefault())
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top