使用Java验证HTTP代理 [英] Authenticated HTTP proxy with Java
问题描述
如何配置用户名和密码以使用Java验证http代理服务器?
How can I configure the username and password to authenticate a http proxy server using Java?
我刚刚找到以下配置参数:
I just found the following configuration parameters:
http.proxyHost=<proxyAddress>
http.proxyPort=<proxyPort>
https.proxyHost=<proxyAddress>
https.proxyPort=<proxyPort>
但是,我的代理服务器需要身份验证。如何配置我的应用程序以使用代理服务器?
But, my proxy server requires authentication. How can I configure my app to use the proxy server?
推荐答案
(编辑:正如OP指出的,使用a java.net.Authenticator
也是必需的。为了正确起见,我正在更新我的答案。)
( As pointed out by the OP, the using a java.net.Authenticator
is required too. I'm updating my answer accordingly for the sake of correctness.)
对于身份验证,请使用 java.net.Authenticator
设置代理的配置并设置系统属性 http.proxyUser
和 http.proxyPassword
。
For authentication, use java.net.Authenticator
to set proxy's configuration and set the system properties http.proxyUser
and http.proxyPassword
.
final String authUser = "user";
final String authPassword = "password";
Authenticator.setDefault(
new Authenticator() {
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
authUser, authPassword.toCharArray());
}
}
);
System.setProperty("http.proxyUser", authUser);
System.setProperty("http.proxyPassword", authPassword);
这篇关于使用Java验证HTTP代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!