将 JVM/JRE 设置为自动使用 Windows 代理

2022-01-16 00:00:00 proxy jvm java

我确实看到了有关为 JVM 设置代理的问题,但我想问的是如何利用已配置的代理(在 Windows 上).

这是我的问题的演示:

<块引用><块引用>

  1. 转到您的控制面板->Java 并设置代理地址.
  2. 运行以下简单的小程序代码(我使用的是 Eclipse IDE):

导入java.awt.Graphics;导入 javax.swing.JApplet;导入 java.util.*;公共类 Stacklet 扩展 JApplet {私人字符串消息;公共无效初始化(){属性 props = System.getProperties();message = props.getProperty("http.proxyHost", "NONE");message = (message.length() == 0)?无":消息;}公共无效油漆(图形g){g.drawString(消息, 20, 20);}}

Applet 显示NONE",与您在 Java 控制面板中放置的设置无关.如果我可以确定 Windows 代理设置(通常在 Internet Explorer 中设置),但在 Java 控制面板中执行额外的配置步骤仍然是可接受的解决方案,那么最好的办法是.

谢谢!

解决方案

可以使用 ProxySelector 类并通过使用 System类的setProperty方法:

System.setProperty("java.net.useSystemProxies", "true");System.out.println("检测代理");列表 l = null;尝试 {l = ProxySelector.getDefault().select(new URI("http://foo/bar"));}捕捉(URISyntaxException e){e.printStackTrace();}如果(l!= null){for (Iterator iter = l.iterator(); iter.hasNext();) {java.net.Proxy 代理 = (java.net.Proxy) iter.next();System.out.println("代理类型:" + proxy.type());InetSocketAddress 地址 = (InetSocketAddress) proxy.address();if (addr == null) {System.out.println("无代理");} 别的 {System.out.println("代理主机名:" + addr.getHostName());System.setProperty("http.proxyHost", addr.getHostName());System.out.println("代理端口:" + addr.getPort());System.setProperty("http.proxyPort", Integer.toString(addr.getPort()));}}}

I did see the question about setting the proxy for the JVM but what I want to ask is how one can utilize the proxy that is already configured (on Windows).

Here is a demonstration of my issue:

  1. Go to your Control Panel->Java and set a proxy address.
  2. Run the following simple applet code (I'm using the Eclipse IDE):

import java.awt.Graphics;
import javax.swing.JApplet;
import java.util.*;

public class Stacklet extends JApplet {
    private String message;
    public void init(){
        Properties props = System.getProperties();
        message = props.getProperty("http.proxyHost", "NONE");      
        message = (message.length() == 0)? "NONE": message;
    }

    public void paint(Graphics g)
    {
        g.drawString(message, 20, 20);
    }
}

The Applet displays "NONE" without regard to the settings you've placed in the Java Control Panel. What would be best would be if the Windows proxy settings (usually set in Internet Explorer) were what I could determine but doing an extra configuration step in the Java Control Panel would still be an acceptable solution.

Thanks!

解决方案

It is possible to detect the proxy using the ProxySelector class and assign the system proxy by assigning environment variables with the setProperty method of the System class:

System.setProperty("java.net.useSystemProxies", "true");
System.out.println("detecting proxies");
List l = null;
try {
    l = ProxySelector.getDefault().select(new URI("http://foo/bar"));
} 
catch (URISyntaxException e) {
    e.printStackTrace();
}
if (l != null) {
    for (Iterator iter = l.iterator(); iter.hasNext();) {
        java.net.Proxy proxy = (java.net.Proxy) iter.next();
        System.out.println("proxy type: " + proxy.type());

        InetSocketAddress addr = (InetSocketAddress) proxy.address();

        if (addr == null) {
            System.out.println("No Proxy");
        } else {
            System.out.println("proxy hostname: " + addr.getHostName());
            System.setProperty("http.proxyHost", addr.getHostName());
            System.out.println("proxy port: " + addr.getPort());
            System.setProperty("http.proxyPort", Integer.toString(addr.getPort()));
        }
    }
}

相关文章