我面對的Java6/8的好奇行爲。 我嘗試通過需要基本用戶身份驗證的代理進行隧道傳輸。 這是通過標準的java Authenticator來完成的。 如果我嘗試訪問https網址作爲第一個url,則拋出異常:無法通過代理進行隧道傳輸。代理返回「HTTP/1.1 407」通過https
java.io.IOException:無法通過代理進行隧道傳輸。代理返回「HTTP/1.1 407代理服務器身份驗證」
但是,如果我先訪問HTTP URL的HTTPS URL,HTTPS訪問工作正常。
鑑於代碼:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.net.Proxy;
import java.net.URL;
public class ProxyPass {
public ProxyPass(String proxyHost, int proxyPort, final String userid, final String password, String url) {
try {
/* Create a HttpURLConnection Object and set the properties */
URL u = new URL(url);
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));
HttpURLConnection uc = (HttpURLConnection) u.openConnection(proxy);
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
if (getRequestorType().equals(RequestorType.PROXY)) {
return new PasswordAuthentication(userid, password.toCharArray());
}
return super.getPasswordAuthentication();
}
});
uc.connect();
/* Print the content of the url to the console. */
showContent(uc);
}
catch (IOException e) {
e.printStackTrace();
}
}
private void showContent(HttpURLConnection uc) throws IOException {
InputStream i = uc.getInputStream();
char c;
InputStreamReader isr = new InputStreamReader(i);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
public static void main(String[] args) {
String proxyhost = "proxyHost";
int proxyport = proxyPort;
final String proxylogin = proxyUser;
final String proxypass = proxyPass;
String url = "http://www.google.de";
String surl = "https://www.google.de";
// new ProxyPass(proxyhost, proxyport, proxylogin, proxypass, url); // uncomment this line to see that the https request works!
// System.out.println(url + " ...ok"); // uncomment this line to see that the https request works!
new ProxyPass(proxyhost, proxyport, proxylogin, proxypass, surl);
System.out.println(surl + " ...ok");
}
任何建議,想法?
因此,您必須在每次JDK更新後執行此操作? – mbee