很多时候,Java应用程序需要连接到Internet。最常见的例子发生在读取XML文件并需要下载其模式时。
我在代理服务器后面。如何将JVM设置为使用代理?
很多时候,Java应用程序需要连接到Internet。最常见的例子发生在读取XML文件并需要下载其模式时。
我在代理服务器后面。如何将JVM设置为使用代理?
当前回答
从Java文档(不是javadoc API):
http://download.oracle.com/javase/6/docs/technotes/guides/net/proxies.html
设置JVM标志http。proxyHost和http。在命令行上启动JVM时使用proxyPort。 这通常是在shell脚本(Unix)或bat文件(Windows)中完成的。下面是Unix shell脚本的示例:
JAVA_FLAGS=-Dhttp.proxyHost=10.0.0.100 -Dhttp.proxyPort=8800
java ${JAVA_FLAGS} ...
当使用JBoss或WebLogic等容器时,我的解决方案是编辑供应商提供的启动脚本。
许多开发人员都熟悉Java API (javadocs),但很多时候忽略了文档的其余部分。它包含许多有趣的信息:http://download.oracle.com/javase/6/docs/technotes/guides/
更新:如果您不想使用代理来解析一些本地/内网主机,请查看来自@Tomalak的评论:
不要忘记http。nonProxyHosts财产!
-Dhttp.nonProxyHosts="localhost|127.0.0.1|10.*.*.*|*.example.com|etc"
其他回答
下面展示了如何在Java中通过命令行设置具有代理用户和代理密码的代理,这是一种非常常见的情况。首先,您不应该在代码中保存密码和主机,这是一个规则。
在命令行中使用-D传递系统属性,并使用system在代码中设置它们。setProperty("name", "value")是等价的。
但是请注意
工作的例子:
C:\temp>java -Dhttps.proxyHost=host -Dhttps.proxyPort=port -Dhttps.proxyUser=user -Dhttps.proxyPassword="password" -Djavax.net.ssl.trustStore=c:/cacerts -Djavax.net.ssl.trustStorePassword=changeit com.andreas.JavaNetHttpConnection
但是下面的方法不起作用:
C:\temp>java com.andreas.JavaNetHttpConnection -Dhttps.proxyHost=host -Dhttps.proxyPort=port -Dhttps=proxyUser=user -Dhttps.proxyPassword="password" -Djavax.net.ssl.trustStore=c:/cacerts -Djavax.net.ssl.trustStorePassword=changeit
唯一的区别是系统属性的位置!(上课前后)
如果你的密码中有特殊字符,你可以把它放在引号中“@MyPass123%”,就像上面的例子一样。
访问HTTPS服务时,必须使用HTTPS协议。proxyHost, https。proxyPort等等。
如果访问HTTP服务,必须使用HTTP。proxyHost, http。proxyPort等等。
你可以通过编程的方式设置这些标志:
if (needsProxy()) {
System.setProperty("http.proxyHost",getProxyHost());
System.setProperty("http.proxyPort",getProxyPort());
} else {
System.setProperty("http.proxyHost","");
System.setProperty("http.proxyPort","");
}
只要从方法needsProxy(), getProxyHost()和getProxyPort()中返回正确的值,您就可以随时调用此代码片段。
这是一个小更新,但是从Java 7开始,现在可以通过编程方式创建代理连接,而不是通过系统属性。这可能是有用的,如果:
代理需要在程序运行时动态旋转 需要使用多个并行代理 或者只是让你的代码更干净:)
这是groovy中一个人为的例子:
// proxy configuration read from file resource under "proxyFileName"
String proxyFileName = "proxy.txt"
String proxyPort = "1234"
String url = "http://www.promised.land"
File testProxyFile = new File(proxyFileName)
URLConnection connection
if (!testProxyFile.exists()) {
logger.debug "proxyFileName doesn't exist. Bypassing connection via proxy."
connection = url.toURL().openConnection()
} else {
String proxyAddress = testProxyFile.text
connection = url.toURL().openConnection(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyAddress, proxyPort)))
}
try {
connection.connect()
}
catch (Exception e) {
logger.error e.printStackTrace()
}
全部参考: http://docs.oracle.com/javase/7/docs/technotes/guides/net/proxies.html
可以将代理服务器的一些属性设置为jvm参数
-Dhttp。proxyPort=8080, proxyHost,等等。
但是如果你需要通过一个认证代理,你需要一个像下面这样的验证器:
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
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
}
}
根据这个回复: http://mail-archives.apache.org/mod_mbox/jakarta-jmeter-user/200208.mbox/%3C494FD350388AD511A9DD00025530F33102F1DC2C@MMSX006%3E
从Java文档(不是javadoc API):
http://download.oracle.com/javase/6/docs/technotes/guides/net/proxies.html
设置JVM标志http。proxyHost和http。在命令行上启动JVM时使用proxyPort。 这通常是在shell脚本(Unix)或bat文件(Windows)中完成的。下面是Unix shell脚本的示例:
JAVA_FLAGS=-Dhttp.proxyHost=10.0.0.100 -Dhttp.proxyPort=8800
java ${JAVA_FLAGS} ...
当使用JBoss或WebLogic等容器时,我的解决方案是编辑供应商提供的启动脚本。
许多开发人员都熟悉Java API (javadocs),但很多时候忽略了文档的其余部分。它包含许多有趣的信息:http://download.oracle.com/javase/6/docs/technotes/guides/
更新:如果您不想使用代理来解析一些本地/内网主机,请查看来自@Tomalak的评论:
不要忘记http。nonProxyHosts财产!
-Dhttp.nonProxyHosts="localhost|127.0.0.1|10.*.*.*|*.example.com|etc"