我确实看到了关于为JVM设置代理的问题,但我想问的是如何利用已经配置的代理(在Windows上).
这是我的问题的演示:
转到控制面板 - > Java并设置代理地址.
运行以下简单的applet代码(我正在使用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); } }
Applet显示"NONE",而不考虑您在Java控制面板中放置的设置.如果Windows代理设置(通常在Internet Explorer中设置)是我可以确定的,但在Java控制面板中执行额外的配置步骤仍然是可接受的解决方案,那将是最好的.
谢谢!
可以使用ProxySelector类检测代理,并通过使用System类的setProperty方法分配环境变量来分配系统代理:
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())); } } }
我在这里发现了一个奇怪的行为,试验了建议的代码.
看来,在设置了默认的ProxySelector之后,常规套接字代码(例如创建一个新的Socket)不再起作用了,因为它试图使用socks服务器(不知道为什么会这样做,但对我来说它确实如此) .
所以,如果你,在打电话时
Socket socket = new Socket(host, port);
你收到这样的SocketException:
java.net.SocketException: Malformed reply from SOCKS server at java.net.SocksSocketImpl.readSocksReply(Unknown Source) at java.net.SocksSocketImpl.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at java.net.Socket.(Unknown Source) at java.net.Socket. (Unknown Source)
然后尝试将默认的ProxySelector设置为null:
ProxySelector.setDefault(null);
对我来说,这导致了以下小型Java类,我现在用它来简单地检索系统代理设置,而不会影响应用程序的Sockets()的进一步使用,同时正确配置系统以使用代理:
public class ProxyConfig { private static String host; private static int port; public static void init() { System.setProperty("java.net.useSystemProxies", "true"); Proxy proxy = getProxy(); if (proxy != null) { InetSocketAddress addr = (InetSocketAddress) proxy.address(); host = addr.getHostName(); port = addr.getPort(); System.setProperty("java.net.useSystemProxies", "false"); System.setProperty("http.proxyHost", host); System.setProperty("http.proxyPort", ""+port); } System.setProperty("java.net.useSystemProxies", "false"); } public static String getHost() { return host; } public static int getPort() { return port; } private static Proxy getProxy() { Listl = null; try { ProxySelector def = ProxySelector.getDefault(); l = def.select(new URI("http://foo/bar")); ProxySelector.setDefault(null); } catch (Exception e) { e.printStackTrace(); } if (l != null) { for (Iterator iter = l.iterator(); iter.hasNext();) { java.net.Proxy proxy = iter.next(); return proxy; } } return null; } }
这可能有点晚了,但我遇到了同样的问题.我修复它的方法是使用-Djava.net.useSystemProxies = true作为jvm参数.
"请注意,此属性仅在启动时检查一次."
此属性在启动时设置,因此在运行应用程序时不会更改.
我希望这有帮助.
参考:https: //docs.oracle.com/javase/7/docs/api/java/net/doc-files/net-properties.html