java Java系统属性,http.proxyHost,两个问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13742350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java system Properties, http.proxyHost, two questions
提问by The111
I am developing a Java application that makes HTTP requests, and half of my development time is behind a proxy. So I have the following block in my code:
我正在开发一个发出 HTTP 请求的 Java 应用程序,我的一半开发时间都在代理后面。所以我的代码中有以下块:
if (BEHIND_PROXY) {
java.util.Properties systemProperties = System.getProperties();
systemProperties.setProperty("http.proxyHost", PROXY_HOST);
systemProperties.setProperty("http.proxyPort", PROXY_PORT);
}
The idea is that I change the value of BEHIND_PROXY
based on where I am. I was working today, notbehind a proxy, and forgot to set BEHIND_PROXY
to false
. However, the connection was still made successfully and my application received the data it requested. How is this possible? Is there something built into this, that if the proxy server cannot be reached, it simply tries again but bypasses the proxy on this retry?
这个想法是我BEHIND_PROXY
根据我所在的位置更改 的值。我今天正在工作,而不是在代理后面,忘记设置BEHIND_PROXY
为false
. 但是,连接仍然成功,我的应用程序收到了它请求的数据。这怎么可能?是否有内置的东西,如果无法访问代理服务器,它只是再次尝试但在这次重试时绕过代理?
And a second question, I have been trying to find a complete list of system properties. I have found many posts like THISone, but not one of them lists http.proxyHost
or http.proxyPort
, which makes me think they are clearly not very complete. Am I searching wrong somehow? Do these http.x
properties belong in these other lists? Is there a more complete list somewhere?
第二个问题,我一直在尝试查找系统属性的完整列表。我找到了很多像THISone这样的帖子,但没有一个列出http.proxyHost
或http.proxyPort
,这让我觉得它们显然不是很完整。我以某种方式搜索错误了吗?这些http.x
属性是否属于这些其他列表?某处有更完整的列表吗?
回答by Nathaniel Waisbrot
Is there something built into this, that if the proxy server cannot be reached, it simply tries again but bypasses the proxy on this retry?
是否有内置的东西,如果无法访问代理服务器,它只是再次尝试但在这次重试时绕过代理?
Yes.
是的。
I was surprised to see this, but here it is in the source of the internal connection: sun.net.www.protocol.http.HttpURLConnection. On line 760, if we've tried all the available proxies and failed to connect, we try a non-proxied connection.
看到这个我很惊讶,但这里是内部连接的来源: sun.net.www.protocol.http.HttpURLConnection。在第 760 行,如果我们尝试了所有可用的代理但连接失败,我们将尝试非代理连接。
Am I searching wrong somehow?
我以某种方式搜索错误了吗?
Maybe. Right or wrong, the Java philosophy seems to be that system properties are ad-hoc things and the only way to know that one exists is to read the documentation for the thing that it affects. In this case, HttpURLConnection links to a page on Networking Properties.
或许。对或错,Java 哲学似乎是系统属性是临时的东西,知道一个存在的唯一方法是阅读它影响的东西的文档。在这种情况下, HttpURLConnection 链接到Networking Properties上的页面。