我正在尝试根据How do a send an HTTPS request through a proxy in Java?使用代理访问https网页
但是我遇到了一个奇怪的问题: HttpsURLConnection忽略了setSSLSocketFactory方法。以下是我的代码:
HttpsURLConnection connection = (HttpsURLConnection) new URL("https://www.google.com").openConnection();
connection.setSSLSocketFactory(new SSLTunnelSocketFactory("127.0.0.1", 8089));这是SSLTunnelSocketFactory类:
class SSLTunnelSocketFactory extends SSLSocketFactory {
public SSLTunnelSocketFactory(String proxyIpAddress, int proxyPort) {
System.out.println("calling SSLTunnelSocketFactory");
}
@Override
public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException, UnknownHostException {
System.out.println("calling createSocket");
}
@Override
public Socket createSocket(String s, int i) throws IOException,
UnknownHostException {
throw new NotImplementedException();
}
@Override
public Socket createSocket(String s, int i, InetAddress inetAddress,
int i2) throws IOException,
UnknownHostException {
throw new NotImplementedException();
}
@Override
public Socket createSocket(InetAddress inetAddress,
int i) throws IOException {
throw new NotImplementedException();
}
@Override
public Socket createSocket(InetAddress inetAddress, int i,
InetAddress inetAddress2,
int i2) throws IOException {
throw new NotImplementedException();
}
@Override
public String[] getDefaultCipherSuites() {
throw new NotImplementedException();
}
@Override
public String[] getSupportedCipherSuites() {
throw new NotImplementedException();
}
}下面是输出:
calling SSLTunnelSocketFactory
Exception in thread "main" java.lang.RuntimeException: java.net.ConnectException: Connection timed out: connect您可以看到"calling createSocket“没有打印出来,并且发生了超时异常。这意味着setSSLSocketFactory方法被忽略,这是不应该发生的。
你能帮我解决这个问题吗?
发布于 2014-09-07 13:37:22
似乎需要设置connection.setDoInput(true);connection.setDoOutput(true);
请参阅此解决方案-https://community.oracle.com/thread/1536034?start=0&tstart=0
https://stackoverflow.com/questions/25707190
复制相似问题