JavaFX webview set Proxy

2019-06-08 12:33发布

问题:

I'm using a JavaFX webview in my application. Inside of it I load a local html file, which itself loads some javascript library from maps.google.com and then displays a google map with some markers inside the webview.

Now according to this question if I want to use a proxy I should just do:

System.setProperty("http.proxyHost","proxy.esrf.fr");
System.setProperty("http.proxyPort","3128");

But this has no effect. I can set whatever I want as the host and port. The google map tiles are still loaded, even if the settings are no valid proxy. So apparently it is not making use of the proxy settings.

How can I make sure, that all web traffic within the WebView is going via the proxy.

I also set the https.proxyHost and https.proxyPort by the way, just in case.

Thanks!

回答1:

The problem was cause by a bugfix I did earlier. When working with proxies in java this post is quite helpful. In the end it recommends setting ProxySelector.setDefault(null); to avoid issue with sockets and proxies.

However, this made setting the proxy via

System.setProperty("http.proxyHost","proxy.esrf.fr");
System.setProperty("http.proxyPort","3128");

impossible. Actually, it wasn't possible to set any proxy at all except by passing a Proxy object to URL.openConnection(Proxy p);

So instead of setting the default ProxySelector to null, I recommend setting

ProxySelector.setDefault(ProxySelector.getDefault());