我正在寻找一种以编程方式更改Windows Internet选项的方法(代理设置更具体)。我在C#中看到有一个名为InternetSetOption的类,我相信它确实需要我。我想知道是否有Java等价物?
如果没有,无论如何我可以在Java中更改Windows网络代理设置,因为更改注册表可以工作,但您需要重新启动或重新启动explorer.exe,这不是此应用程序的选项。
答案 0 :(得分:0)
我想我找到了你要找的东西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" );