我使用代理服务器。如何设置我的 JVM 使用代理?
根据Java文档(不是javadoc API):
http://download.oracle.com/javase/6/docs/technotes/guides/net/proxies.html
在命令行中启动JVM时,设置http.proxyHost
和http.proxyPort
JVM标志。
这通常在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"
http.proxyUser
和 http.proxyPassword
不是 Java 系统属性,它们是用于 Apache HTTP 客户端的。 - user207421https.proxyHost
和https.proxyPort
。 - C-Otto使用系统代理设置:
java -Djava.net.useSystemProxies=true ...
或者以编程方式:
System.setProperty("java.net.useSystemProxies", "true");
来源:http://docs.oracle.com/javase/7/docs/api/java/net/doc-files/net-properties.html
编程设置HTTP/HTTPS和/或SOCKS代理:
...
public void setProxy() {
if (isUseHTTPProxy()) {
// HTTP/HTTPS Proxy
System.setProperty("http.proxyHost", getHTTPHost());
System.setProperty("http.proxyPort", getHTTPPort());
System.setProperty("https.proxyHost", getHTTPHost());
System.setProperty("https.proxyPort", getHTTPPort());
if (isUseHTTPAuth()) {
String encoded = new String(Base64.encodeBase64((getHTTPUsername() + ":" + getHTTPPassword()).getBytes()));
con.setRequestProperty("Proxy-Authorization", "Basic " + encoded);
Authenticator.setDefault(new ProxyAuth(getHTTPUsername(), getHTTPPassword()));
}
}
if (isUseSOCKSProxy()) {
// SOCKS Proxy
System.setProperty("socksProxyHost", getSOCKSHost());
System.setProperty("socksProxyPort", getSOCKSPort());
if (isUseSOCKSAuth()) {
System.setProperty("java.net.socks.username", getSOCKSUsername());
System.setProperty("java.net.socks.password", getSOCKSPassword());
Authenticator.setDefault(new ProxyAuth(getSOCKSUsername(), getSOCKSPassword()));
}
}
}
...
public class ProxyAuth extends Authenticator {
private PasswordAuthentication auth;
private ProxyAuth(String user, String password) {
auth = new PasswordAuthentication(user, password == null ? new char[]{} : password.toCharArray());
}
protected PasswordAuthentication getPasswordAuthentication() {
return auth;
}
}
...
请记住,HTTP代理和SOCKS代理在网络堆栈中的不同层级上运行,因此您可以使用其中一个或两者兼用。
您可以通过编程的方式设置这些标志:
if (needsProxy()) {
System.setProperty("http.proxyHost",getProxyHost());
System.setProperty("http.proxyPort",getProxyPort());
} else {
System.setProperty("http.proxyHost","");
System.setProperty("http.proxyPort","");
}
只需从needsProxy()
、getProxyHost()
和getProxyPort()
方法中返回正确的值,您就可以在任何时候调用此代码片段。
JVM使用代理进行HTTP调用。
System.getProperties().put("http.proxyHost", "someProxyURL");
System.getProperties().put("http.proxyPort", "someProxyPort");
这可能会使用用户设置的代理
System.setProperty("java.net.useSystemProxies", "true");
System.setProperty
代替System.getProperties().put(...)
。 - Simon Forsberg将 java.net.useSystemProxies
属性设置为 true
。例如,您可以通过 JAVA_TOOL_OPTIONS 环境变量来设置它。在Ubuntu中,您可以将以下行添加到 .bashrc
文件中:
export JAVA_TOOL_OPTIONS+=" -Djava.net.useSystemProxies=true"
ARG _JAVA_OPTIONS="-Dhttps.proxyHost=myproxy -Dhttps.proxyPort=myport"
才能生效。但硬编码并不可行。 - user3999721将Sorter和javabrett/Leonel的答案结合起来:
java -Dhttp.proxyHost=10.10.10.10 -Dhttp.proxyPort=8080 -Dhttp.proxyUser=username -Dhttp.proxyPassword=password -jar myJar.jar
你可以将一些代理服务器的属性设置为jvm参数
-Dhttp.proxyPort=8080, proxyHost等等。
但是,如果您需要通过身份验证代理,您需要像这个示例中的Authenticator:
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
}
}
Based on this reply: http://mail-archives.apache.org/mod_mbox/jakarta-jmeter-user/200208.mbox/%3C494FD350388AD511A9DD00025530F33102F1DC2C@MMSX006%3EC:\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
唯一的区别是系统属性的位置!(在类之前和之后)。https.proxyHost
,https.proxyPort
等。http.proxyHost
,http.proxyPort
等。java -DsocksProxyHost=127.0.0.1 -DsocksProxyPort=8080 org.example.Main