从公司的私有云机房迁到公有云机房后,对外是五个公网IP随机呈现,购买的代理IP供应商只能绑定一个授权公网IP,因此只能采取帐密方式。
针对http协议的请求通过:
proxyAuth = new String(Base64.encodeBase64(proxyUser + ":" + proxyPassword));
conn = (HttpURLConnection) url.openConnection(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(haip, port));
if (proxyAuth != null) conn.setRequestProperty("Proxy-Authorization", "Basic " + proxyAuth);
这种方式即可。
但是请求https时报错:Proxy returns "HTTP/1.0 407 Proxy Authentication Required",添加了ssl后依然有问题
proxyAuth = new String(Base64.encodeBase64(proxyUser + ":" + proxyPassword));
conn = (HttpsURLConnection) url.openConnection(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(haip, port));
if (proxyAuth != null) conn.setRequestProperty("Proxy-Authorization", "Basic " + proxyAuth);
SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, new TrustManager[] {new TrustAnyTrustManager()}, new java.security.SecureRandom());
conn.setSSLSocketFactory(sslContext.getSocketFactory());
后来翻阅多方材料后发现以一下代码方式可以实现:
System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");
Authenticator.setDefault(
new Authenticator() {
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(authUser, authPassword.toCharArray());
}
}
);
谢谢~