很多时候,Java 应用程序需要连接到 Internet。最常见的示例发生在读取 XML 文件并需要下载其模式时。
我在代理服务器后面。如何设置我的 JVM 以使用代理?
从 Java 文档(不是 javadoc API):
http://download.oracle.com/javase/6/docs/technotes/guides/net/proxies.html
在命令行上启动 JVM 时设置 JVM 标志 http.proxyHost
和 http.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 -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(...)
结合 Sorter 和 javabrett/Leonel 的答案:
java -Dhttp.proxyHost=10.10.10.10 -Dhttp.proxyPort=8080 -Dhttp.proxyUser=username -Dhttp.proxyPassword=password -jar myJar.jar
将 java.net.useSystemProxies
属性设置为 true
。例如,您可以通过 JAVA_TOOL_OPTIONS 环境变量来设置它。例如,在 Ubuntu 中,您可以将以下行添加到 .bashrc
:
导出 JAVA_TOOL_OPTIONS+="-Djava.net.useSystemProxies=true"
您可以将有关代理服务器的一些属性设置为 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
}
}
下面展示了如何在 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
等。
读取 XML 文件并需要下载其架构
如果您指望通过 Internet 检索模式或 DTD,那么您正在构建一个缓慢、健谈、脆弱的应用程序。当托管文件的远程服务器发生计划内或计划外停机时会发生什么?您的应用程序中断。那样行吗?
请参阅http://xml.apache.org/commons/components/resolver/resolver-article.html#s.catalog.files
架构等的 URL 最好被视为唯一标识符。不像请求实际远程访问该文件。在“XML 目录”上做一些谷歌搜索。 XML 目录允许您在本地托管此类资源,从而解决缓慢、繁琐和脆弱的问题。
它基本上是远程内容的永久缓存副本。没关系,因为远程内容永远不会改变。如果有更新,它将位于不同的 URL。通过互联网实际检索资源特别愚蠢。
如果您想要“Socks 代理”,请通知“socksProxyHost”和“socksProxyPort”VM 参数。
例如
java -DsocksProxyHost=127.0.0.1 -DsocksProxyPort=8080 org.example.Main
我也在防火墙后面,这对我有用!
System.setProperty("http.proxyHost", "proxy host addr");
System.setProperty("http.proxyPort", "808");
Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("domain\\user","password".toCharArray());
}
});
URL url = new URL("http://www.google.com/");
URLConnection con = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
// Read it ...
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
在连接到代理后面的 URL 之前添加它。
System.getProperties().put("http.proxyHost", "someProxyURL");
System.getProperties().put("http.proxyPort", "someProxyPort");
System.getProperties().put("http.proxyUser", "someUserName");
System.getProperties().put("http.proxyPassword", "somePassword");
http.proxyUser
和 http.proxyPassword
不是 Java 系统属性。它们用于 Apache HTTP 客户端。
System.setProperty
而不是 System.getProperties().put(...)
这是一个小更新,但从 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 使用浏览器代理设置的方法。您需要做的是将 ${java.home}/lib/deploy.jar
添加到您的项目并像下面这样初始化库:
import com.sun.deploy.net.proxy.DeployProxySelector;
import com.sun.deploy.services.PlatformType;
import com.sun.deploy.services.ServiceManager;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public abstract class ExtendedProxyManager {
private static final Log logger = LogFactory.getLog(ExtendedProxyManager.class);
/**
* After calling this method, proxy settings can be magically retrieved from default browser settings.
*/
public static boolean init() {
logger.debug("Init started");
// Initialization code was taken from com.sun.deploy.ClientContainer:
ServiceManager
.setService(System.getProperty("os.name").toLowerCase().indexOf("windows") != -1 ? PlatformType.STANDALONE_TIGER_WIN32
: PlatformType.STANDALONE_TIGER_UNIX);
try {
// This will call ProxySelector.setDefault():
DeployProxySelector.reset();
} catch (Throwable throwable) {
logger.error("Unable to initialize extended dynamic browser proxy settings support.", throwable);
return false;
}
return true;
}
}
之后,代理设置可通过 java.net.ProxySelector
用于 Java API。
这种方法的唯一问题是您需要在引导类路径中使用 deploy.jar
启动 JVM,例如 java -Xbootclasspath/a:"%JAVA_HOME%\jre\lib\deploy.jar" -jar my.jar
。如果有人知道如何克服这个限制,请告诉我。
xbootclasspath
会有什么效果,我不能把那个 jar 放到我的正常类路径中(在没有 webstart 的情况下运行时)吗?
Exception in thread "main" java.lang.IllegalAccessError: class ...) cannot access class com.sun.deploy.net.proxy.DeployProxySelector (in module jdk.deploy) because module jdk.deploy does not export com.sun.deploy.net.proxy
这对我行得通:
public void setHttpProxy(boolean isNeedProxy) {
if (isNeedProxy) {
System.setProperty("http.proxyHost", getProxyHost());
System.setProperty("http.proxyPort", getProxyPort());
} else {
System.clearProperty("http.proxyHost");
System.clearProperty("http.proxyPort");
}
}
P/S:我基于 GHad 的回答。
正如其他答案中指出的那样,如果您需要使用经过身份验证的代理,则没有可靠的方法来纯粹使用命令行变量来做到这一点 - 如果您使用的是其他人的应用程序并且不想弄乱源代码。
Will Iverson 在 Using HttpProxy to connect to a host with preemtive authentication 上提出了有用的建议,即使用代理管理工具,例如 Proxifier(Mac OS X 和 Windows 的 http://www.proxifier.com/)来处理这个问题。
例如,使用 Proxifier,您可以将其设置为仅拦截要通过其(经过身份验证的)代理管理和重定向的 java 命令。在这种情况下,您需要将 proxyHost 和 proxyPort 值设置为空白,例如将 -Dhttp.proxyHost= -Dhttp.proxyPort=
传递给您的 java 命令。
这是一个对我有用的完整示例 - 请注意,对于 HTTPS,有单独的属性(根据 https://docs.oracle.com/javase/8/docs/technotes/guides/net/proxies.html)。
下面的代码向 https://api.myip.com API 发送请求并打印响应。
public static void main(String[] args) throws IOException {
System.setProperty("java.net.useSystemProxies", "true");
final String proxyUser = "lum-customer-c_f95783f5-zone-datacenter-ip-92.240.207.56";
final String proxyPass = "s3mgwrx2b628";
final String host = "zproxy.lum-superproxy.io";
final Integer port = 22225;
// http
System.setProperty("http.proxyHost",host);
System.setProperty("http.proxyPort", String.valueOf(port));
System.setProperty("http.proxyUser", proxyUser);
System.setProperty("http.proxyPassword", proxyPass);
// https
System.setProperty("https.proxyHost",host);
System.setProperty("https.proxyPort", String.valueOf(port));
System.setProperty("https.proxyUser", proxyUser);
System.setProperty("https.proxyPassword", proxyPass);
System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");
System.setProperty("jdk.https.auth.tunneling.disabledSchemes", "");
Authenticator.setDefault(new Authenticator() {
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(proxyUser, proxyPass.toCharArray());
}
}
);
// create and send a https request to myip.com API
URL url = new URL("https://api.myip.com");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
int status = connection.getResponseCode();
// read the response
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String responseLine;
StringBuffer responseContent = new StringBuffer();
while ((responseLine = in.readLine()) != null)
responseContent.append(responseLine);
in.close();
connection.disconnect();
// print the response
System.out.println(status);
System.out.println(responseContent);
}
如果您在独立的 JVM 中,您可以使用 http.proxy* JVM 变量,但您不应该修改它们的启动脚本和/或在您的应用程序服务器中执行此操作(可能除了 jboss 或 tomcat)。相反,您应该使用 JAVA 代理 API(而不是 System.setProperty)或使用供应商自己的配置选项。 WebSphere 和 WebLogic 都有非常明确的方法来设置比 J2SE 强大得多的代理。此外,对于 WebSphere 和 WebLogic,您可能会通过覆盖启动脚本(特别是服务器的互操作进程,因为您可能会告诉他们也使用您的代理...)来破坏您的应用程序服务器。
不定期副业成功案例分享
http.proxyUser
和http.proxyPassword
不是 Java 系统属性。它们用于 Apache HTTP 客户端。https.proxyHost
和https.proxyPort
。