【问题标题】:Java Equivilent of InternetSetOptionInternetSetOption 的 Java 等价物
【发布时间】:2016-02-10 18:15:45
【问题描述】:

我正在寻找一种以编程方式更改 Windows Internet 选项的方法(代理设置更具体)。我在 C# 中看到有一个名为 InternetSetOption 的类,我相信我确实需要它。我想知道是否有Java等价物?

如果没有,我可以在 Java 中立即更改 Windows 网络代理设置,因为更改注册表会起作用,但您需要重新启动或重新启动 explorer.exe,这不是此应用程序的选项。

【问题讨论】:

    标签: java c# windows proxy


    【解决方案1】:

    我想我找到了你要找的东西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" );
    

    【讨论】:

    • 我相信这只会为 Java 虚拟机设置代理,我需要更改 Windows 系统代理(位于 Internet 选项控制面板中)。
    • 请查看 EDIT 2,这可能是您想要的更多内容
    • 我现在意识到这并没有太大的变化,为什么你需要设置系统属性顺便说一句?
    • 我正在编写一个程序,它与远程服务器建立 SSH 连接,然后将该连接设置为 127.0.0.1:8080 上的代理服务器,然后在操作系统中更改系统代理设置以查找127.0.0.1:8080 上的 SOCKS 代理 这在 Mac 和 Linux 上运行良好,因为您可以发出命令来实现它,windows 一直很痛苦。
    • 这就是我此刻正在研究的内容。我知道我们之前尝试过,但遇到了一些问题(不确定 100% 是什么),但我会尝试更多地与之抗争。
    猜你喜欢
    • 2012-06-03
    • 2013-01-15
    • 1970-01-01
    • 2010-11-13
    • 2019-11-03
    • 2011-01-16
    • 1970-01-01
    • 2016-07-16
    • 2011-10-06
    相关资源
    最近更新 更多