系统代理设置检测失败
我在系统代理检测方面遇到一些神秘的问题: 实际上,我有正确的代码来在运行时检测系统代理设置,它也可以处理 pac 文件和 http 代理设置。 当我在目标站上存储并执行所有内容时,它的工作绝对正确。
但是:我在目标站上运行一小段代码,并将其他任何内容(jar)存储在另一个运行 apache Web 服务器的站上。我从 main 中使用 URLClassloader 加载类,以便通过网络加载该代码段,它也负责代理检测。并且通过这种方式,默认代理选择器始终给予 DIRECT,它无法找到正确的设置。
我认为,问题是我想
System.setProperty("java.net.useSystemProxies","true");
在加载的类中设置,但不知何故这不起作用......但是当我sysout属性值时,它是true
。
我写了一个小测试程序,唯一的区别是:
A)
Class.forname("a"); -> a is in the classpath
B)
URL[] url = new URL[1];
url[0] = new URL("http://1.2.3.4/dtfw");
URLClassloader u = new URLClassloader(url);
Class.forname("a", true, u);
这两段代码都可以工作,唯一的不同在于输出。 有人有什么想法吗?
提前致谢!!
佐米
I have some misterious problem with system proxy detection:
Actually, I have the right code to detect system proxy settings at runtime, it can handle pac files and http proxy settings as well.
It works absolutely correct, when I store and execute everything on the target station.
BUT: I run one little piece of code on the target station and store anything else (jars) on another station, on which apache webserver runs. From the main I load the classes with URLClassloader, so that piece of code loaded via network, which responsible for the proxy detection as well. And in this way the default proxy selector give DIRECT all time, it is not able to find the right settings.
I think, the problem is that I want to set the
System.setProperty("java.net.useSystemProxies","true");
In the loaded class and somehow this does not work...But when I sysout the property value, it is true
.
I wrote a little test program and the only difference is that:
A)
Class.forname("a"); -> a is in the classpath
B)
URL[] url = new URL[1];
url[0] = new URL("http://1.2.3.4/dtfw");
URLClassloader u = new URLClassloader(url);
Class.forname("a", true, u);
Both piece of code work, the only different is in the output.
Does anyone have some idea?
Thanks in advance!!
Zsomi
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了问题的根本原因:
DefaultProxySelector 是每个 JVM 的单例,并且它的行为基于 if System.setProperty("java.net.useSystemProxies","true");是否设置。
当 URLClassloader 使用它时,该值在实例化时为 false。当我在“a”类中尝试使用它时,它无法找到系统代理设置,因为在启动时未设置此属性。
所以我有原因,但还没有解决方案。
问候,
佐米
I found the root cause of the problem:
The DefaultProxySelector is singleton per JVM and the behavior of it based on if System.setProperty("java.net.useSystemProxies","true"); is set or not.
As the URLClassloader uses it, this value is false, when it is instantiated. And when in the "a" class I try to use it, it is not able to find system proxy settings, since at instatiation this property was not set.
So I have the cause, but no solution yet.
Regards,
Zsomi