gpt4 book ai didi

HttpsURLConnection 忽略 setSSLSocketFactory

转载 作者:行者123 更新时间:2023-12-02 02:44:57 29 4
gpt4 key购买 nike

我正在尝试根据 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 方法被忽略,这是不应该发生的。

你能帮我解决这个问题吗?

最佳答案

看来你需要设置 连接.setDoInput(true); 连接.setDoOutput(true);

引用这个解决方案-https://community.oracle.com/thread/1536034?start=0&tstart=0

关于HttpsURLConnection 忽略 setSSLSocketFactory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25707190/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com