HttpsURLConnection 忽略 setSSLSocketFactory
Posted
技术标签:
【中文标题】HttpsURLConnection 忽略 setSSLSocketFactory【英文标题】:HttpsURLConnection ignores setSSLSocketFactory 【发布时间】:2014-10-31 15:57:57 【问题描述】:我正在尝试根据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
可以看到“调用createSocket”没有打印出来,出现超时异常。 这意味着 setSSLSocketFactory 方法被忽略,这不应该发生。
你能帮我解决这个问题吗?
【问题讨论】:
问题解决了吗?如果是这样,请告诉我们解决方案 【参考方案1】:好像你需要设置 连接.setDoInput(true); 连接.setDoOutput(true);
参考这个解决方案-https://community.oracle.com/thread/1536034?start=0&tstart=0
【讨论】:
以上是关于HttpsURLConnection 忽略 setSSLSocketFactory的主要内容,如果未能解决你的问题,请参考以下文章
发生 HttpsURLConnection 400 Bad Request 错误
Java HttpsURLConnection SSLHandshakeException
HttpsURLConnection HttpURLConnection下载图片
如何让 Jersey 客户端使用 HttpsURLConnection 中设置的 defaultSSLSocketFactory?