简体   繁体   English

为什么 ProxySelector 在 Java 中返回不一致的结果?

[英]Why is ProxySelector returning inconsistent results in Java?

8 months ago I used this stack overflow post to automatically use a proxy server in a Java project. 8 个月前,我使用这个堆栈溢出帖子在 Java 项目中自动使用代理服务器。 It worked beautifully.它工作得很好。

Here is the code I came up with at the time:这是我当时想出的代码:

    System.setProperty("java.net.useSystemProxies", "true");

    List<Proxy> proxyServers = null;
    try {
        proxyServers = ProxySelector.getDefault().select(new URI("http://www.twitter.com"));
    } catch (URISyntaxException e) {
      System.out.println("Error using system proxy");
    }
    if (proxyServers != null) {
        for (Iterator<Proxy> iter = proxyServers.iterator(); iter.hasNext();) {
            Proxy proxy = iter.next();
            System.out.println("Found Proxy: " + proxy);
            InetSocketAddress addr = (InetSocketAddress) proxy.address();

            if (addr == null) {
              System.out.println("No Proxy");
            } else {

                System.setProperty("http.proxyHost", addr.getHostName());
                System.setProperty("http.proxyPort", Integer.toString(addr.getPort()));

                System.out.println("proxy hostname : " + addr.getHostName());
                System.out.println("proxy port : " + addr.getPort());

            }
        }
    }

I tried adding this exact same code to another project recently and I get different results.我最近尝试将这个完全相同的代码添加到另一个项目中,但得到了不同的结果。 I have my computer setup to use a test proxy server and ran both projects to compare the results.我的计算机设置为使用测试代理服务器并运行两个项目来比较结果。 If I run the project from 8 months ago the following is printed out:如果我从 8 个月前开始运行该项目,则会打印出以下内容:

Found Proxy: HTTP @ 192.168.1.100:8000
proxy hostname : 192.168.1.100
proxy port : 8000

If I run my current project on the same machine with the same Proxy server set up, the following is printed out.如果我在设置了相同代理服务器的同一台机器上运行我当前的项目,则会打印出以下内容。

Found Proxy: DIRECT 
No Proxy

The only proxy found is a "Direct" and proxy.address() is null.找到的唯一代理是“Direct”,并且 proxy.address() 为空。

What would cause this to find the proxy settings in one case but not the other?什么会导致在一种情况下找到代理设置而不是另一种情况?

Edit:编辑:

I fixed this, by moving the code sooner in the startup process, but I don't know why this fixed it.我通过在启动过程中更快地移动代码来解决这个问题,但我不知道为什么会解决这个问题。

Probably due to this sentence in http://docs.oracle.com/javase/7/docs/api/java/net/doc-files/net-properties.html#Proxies可能是由于http://docs.oracle.com/javase/7/docs/api/java/net/doc-files/net-properties.html#Proxies 中的这句话

java.net.useSystemProxies (default: false) On recent Windows systems and on Gnome 2.x systems it is possible to tell the java.net stack, setting this property to true, to use the system proxy settings (both these systems let you set proxies globally through their user interface). java.net.useSystemProxies(默认值:false)在最近的 Windows 系统和 Gnome 2.x 系统上,可以告诉 java.net 堆栈,将此属性设置为 true,使用系统代理设置(这两个系统都允许您通过用户界面全局设置代理)。 Note that this property is checked only once at startup.请注意,此属性仅在启动时检查一次。

You can guarantee it working if you put the property in the java command: java -Djava.net.useSystemProxies=true yourclass如果将属性放在 java 命令中,则可以保证它正常工作: java -Djava.net.useSystemProxies=true yourclass

Regards问候

If you will use the code along with secure page, it will not consider the proxy hence it would display Direct and No Proxy.如果您将代码与安全页面一起使用,它不会考虑代理,因此它会显示直接和无代理。

Try the below code:试试下面的代码:

proxyServers = ProxySelector.getDefault().select(new URI("http://www.twitter.com"));
proxyServers = ProxySelector.getDefault().select(new URI("https://www.twitter.com"));

you will get the difference.你会得到不同之处。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM