简体   繁体   English

系统代理设置检测失败

[英]system proxy setting detection fails

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.我对系统代理检测有一些神秘的问题:实际上,我有正确的代码来在运行时检测系统代理设置,它也可以处理 pac 文件和 http 代理设置。 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.但是:我在目标站上运行一小段代码,并将其他任何东西(罐子)存储在另一个站上,apache 网络服务器在该站上运行。 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.我主要从 URLClassloader 加载类,以便通过网络加载这段代码,它也负责代理检测。 And in this way the default proxy selector give DIRECT all time, it is not able to find the right settings.并且以这种方式,默认代理选择器始终提供 DIRECT,它无法找到正确的设置。

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 .在加载的 class 中,不知何故这不起作用......但是当我 sysout 属性值时,它是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.这两段代码都有效,唯一不同的是 output。 Does anyone have some idea?有人有什么想法吗?

Thanks in advance!!提前致谢!!

Zsomi佐米

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"); DefaultProxySelector 是 singleton 每个 JVM 和它的行为基于 if System.setProperty("java.net.useSystemProxies","true"); is set or not.设置与否。

As the URLClassloader uses it, this value is false, when it is instantiated.当 URLClassloader 使用它时,该值在实例化时为 false。 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.当我在“a”class 中尝试使用它时,它无法找到系统代理设置,因为在初始化时没有设置此属性。

So I have the cause, but no solution yet.所以我有原因,但还没有解决方案。

Regards, Zsomi问候, 佐米

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

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