设置 JVM/JRE 以自动使用 Windows 代理 [英] Setting JVM/JRE to use Windows Proxy Automatically

查看:34
本文介绍了设置 JVM/JRE 以自动使用 Windows 代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这是我的问题的演示:

<块引用><块引用>

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

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

无论您在 Java 控制面板中的设置如何,Applet 都会显示NONE".如果 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 proxy = (java.net.Proxy) iter.next();System.out.println("代理类型:" + proxy.type());InetSocketAddress addr = (InetSocketAddress) proxy.address();如果(地址 == 空){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()));
        }
    }
}

这篇关于设置 JVM/JRE 以自动使用 Windows 代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆