How to set Http Proxy in an applet

2019-03-25 15:39发布

For a java desktop application after we set these properties

System.setProperty("java.net.useSystemProxies","true");
System.setProperty("http.proxyHost", "1.1.1.1");
System.setProperty("http.proxyPort", "8080");

every http connection will be done through the defined proxy.

But for an applet these does not work.(In an applet viewer it does but in a browser it doesnt.) Applet always uses these settings which are defined in control panel\java\network settings\proxy settings.

How can i set the proxy in an applet? (Using proxy class in every opening connection is not a solution for me.)

Applet is signed and compiled with java 1.6

2条回答
在下西门庆
2楼-- · 2019-03-25 15:53

I imagine that the real reason that the System properties approach doesn't work is that by the time the applet starts, the Java runtime system has already read the properties and set up the default proxy selector.

Have you tried using ProxySelector? Refer to section 4) of this document.

Of course, this is only likely to work when your applet is a signed applet.

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-03-25 15:55

You can do it using API but not for each connection.

Look at URL.openConnection(). It delegates the call to handler. Handler is created by handler factory (if registered). So, you have to register your own factory, create your URL handler that performs URL connection via proxy (calls URL.openConnection(proxy)).

Factory must implement interface URLStreamHandlerFactory and can be registered by calling static method URL.setURLStreamHandlerFactory().

查看更多
登录 后发表回答