【发布时间】:2016-02-10 18:15:45
【问题描述】:
我正在寻找一种以编程方式更改 Windows Internet 选项的方法(代理设置更具体)。我在 C# 中看到有一个名为 InternetSetOption 的类,我相信我确实需要它。我想知道是否有Java等价物?
如果没有,我可以在 Java 中立即更改 Windows 网络代理设置,因为更改注册表会起作用,但您需要重新启动或重新启动 explorer.exe,这不是此应用程序的选项。
【问题讨论】:
我正在寻找一种以编程方式更改 Windows Internet 选项的方法(代理设置更具体)。我在 C# 中看到有一个名为 InternetSetOption 的类,我相信我确实需要它。我想知道是否有Java等价物?
如果没有,我可以在 Java 中立即更改 Windows 网络代理设置,因为更改注册表会起作用,但您需要重新启动或重新启动 explorer.exe,这不是此应用程序的选项。
【问题讨论】:
我想我找到了你要找的东西here。
下面是一些示例代码:
//Set the http proxy to webcache.mydomain.com:8080
System.setProperty("http.proxyHost", "webcache.mydomain.com");
System.setPropery("http.proxyPort", "8080");
// Next connection will be through proxy.
URL url = new URL("http://java.sun.com/");
InputStream in = url.openStream();
// Now, let's 'unset' the proxy.
System.setProperty("http.proxyHost", null);
// From now on http connections will be done directly.
Now,this works reasonably well, even if a bit cumbersome, but it can get tricky if your application is multi-threaded. Remember, system properties are “VM wide” settings, so all threads are affected. Which means that the code in one thread could, as a side effect, render the code in an other thread inoperative.
编辑:
这里有一些更具体的例子:
Let's look at a few examples assuming we're trying to execute the main method of the GetURL class:
$ java -Dhttp.proxyHost=webcache.mydomain.com GetURL
All http connections will go through the proxy server at webcache.mydomain.com listening on port 80 (we didn't specify any port, so the default one is used).
$ java -Dhttp.proxyHost=webcache.mydomain.com -Dhttp.proxyPort=8080
-Dhttp.noProxyHosts=”localhost|host.mydomain.com” GetURL
In that second example, the proxy server will still be at webcache.mydomain.com, but this time listening on port 8080. Also, the proxy won't be used when connecting to either localhost or host.mydonain.com.
编辑 2:
也许是这样的:
System.setProperty( "http.proxyHost", "webcache.mydomain.com" );
System.setProperty( "http.proxyPort", "8080" );
System.setProperty( "https.proxyHost", "webcache.mydomain.com" );
System.setProperty( "https.proxyPort", "8080" );
【讨论】: