本文共 4660 字,大约阅读时间需要 15 分钟。
本文翻译自:
Many times, a Java app needs to connect to the Internet. 很多时候,Java应用程序需要连接到Internet。 The most common example happens when it is reading an XML file and needs to download its schema. 最常见的示例是在读取XML文件并需要下载其架构时发生的。
I am behind a proxy server. 我在代理服务器后面。 How can I set my JVM to use the proxy ? 如何设置我的JVM使用代理?
参考:
To use the system proxy setup: 要使用系统代理设置:
java -Djava.net.useSystemProxies=true ...
Or programatically: 或以编程方式:
System.setProperty("java.net.useSystemProxies", "true");
Source: 来源: :
From the Java documentation ( not the javadoc API): 从Java文档( 不是 javadoc API):
Set the JVM flags http.proxyHost
and http.proxyPort
when starting your JVM on the command line. 在命令行上启动JVM时,设置JVM标志http.proxyHost
和http.proxyPort
。 This is usually done in a shell script (in Unix) or bat file (in Windows). 这通常是在Shell脚本(在Unix中)或bat文件(在Windows中)中完成的。 Here's the example with the Unix shell script: 这是Unix shell脚本的示例:
JAVA_FLAGS=-Dhttp.proxyHost=10.0.0.100 -Dhttp.proxyPort=8800java ${JAVA_FLAGS} ...
When using containers such as JBoss or WebLogic, my solution is to edit the start-up scripts supplied by the vendor. 当使用诸如JBoss或WebLogic之类的容器时,我的解决方案是编辑供应商提供的启动脚本。
Many developers are familiar with the Java API (javadocs), but many times the rest of the documentation is overlooked. 许多开发人员都熟悉Java API(javadocs),但是很多其他文档却被忽略了。 It contains a lot of interesting information: 它包含许多有趣的信息: :
Update : If you do not want to use proxy to resolve some local/intranet hosts, check out the comment from @Tomalak: 更新:如果您不想使用代理来解析某些本地/内联网主机,请查看@Tomalak中的注释:
Also don't forget the http.nonProxyHosts property! 另外,不要忘记http.nonProxyHosts属性!
-Dhttp.nonProxyHosts="localhost|127.0.0.1|10.*.*.*|*.foo.com|etc"
You can set those flags programmatically this way: 您可以通过以下方式以编程方式设置这些标志:
if (needsProxy()) { System.setProperty("http.proxyHost",getProxyHost()); System.setProperty("http.proxyPort",getProxyPort());} else { System.setProperty("http.proxyHost",""); System.setProperty("http.proxyPort","");}
Just return the right values from the methods needsProxy()
, getProxyHost()
and getProxyPort()
and you can call this code snippet whenever you want. 只需从needsProxy()
, getProxyHost()
和getProxyPort()
方法中返回正确的值,就可以随时调用此代码段。
reading an XML file and needs to download its schema 读取XML文件并需要下载其架构
If you are counting on retrieving schemas or DTDs over the internet, you're building a slow, chatty, fragile application. 如果您指望通过Internet检索模式或DTD,那么您正在构建一个缓慢,健谈,易碎的应用程序。 What happens when that remote server hosting the file takes planned or unplanned downtime? 当托管文件的远程服务器发生计划内或计划外停机时,会发生什么情况? Your app breaks. 您的应用中断了。 Is that OK? 这可以吗?
See 参见
URL's for schemas and the like are best thought of as unique identifiers. 最好将模式等的URL视为唯一标识符。 Not as requests to actually access that file remotely. 并非要求实际远程访问该文件。 Do some google searching on "XML catalog". 做一些谷歌搜索“ XML目录”。 An XML catalog allows you to host such resources locally, resolving the slowness, chattiness and fragility. XML目录使您可以在本地托管此类资源,从而解决了速度慢,聊天混乱和脆弱的问题。
It's basically a permanently cached copy of the remote content. 它基本上是远程内容的永久缓存副本。 And that's OK, since the remote content will never change. 没关系,因为远程内容永远不会改变。 If there's ever an update, it'd be at a different URL. 如果有更新,则将使用其他URL。 Making the actual retrieval of the resource over the internet especially silly. 使得通过Internet进行资源的实际检索特别愚蠢。
You can set some properties about the proxy server as jvm parameters 您可以将有关代理服务器的某些属性设置为jvm参数
-Dhttp.proxyPort=8080, proxyHost, etc. -Dhttp.proxyPort = 8080,proxyHost等
but if you need pass through an authenticating proxy, you need an authenticator like this example: 但是如果您需要通过身份验证代理,则需要像以下示例一样的身份验证器:
ProxyAuthenticator.java ProxyAuthenticator.java
import java.net.*;import java.io.*;public class ProxyAuthenticator extends Authenticator { private String userName, password; protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(userName, password.toCharArray()); } public ProxyAuthenticator(String userName, String password) { this.userName = userName; this.password = password; }}
Example.java 范例.java
import java.net.Authenticator; import ProxyAuthenticator;public class Example { public static void main(String[] args) { String username = System.getProperty("proxy.authentication.username"); String password = System.getProperty("proxy.authentication.password"); if (username != null && !username.equals("")) { Authenticator.setDefault(new ProxyAuthenticator(username, password)); } // here your JVM will be authenticated }}
Based on this reply: 根据此回复: :
转载地址:http://yzjnb.baihongyu.com/