废话不多说直接上代码
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
public class Application {
public static void main(String[] args){
final HttpRequest get = HttpUtil.createGet("https://www.example.com");
//new的是下面的类
get.setSSLSocketFactory(new ExampleSSLFactory());
final HttpResponse executeRes = get.execute();
String res = executeRes.body();
System.out.println(res);
}
}
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.net.SSLUtil;
import cn.hutool.core.util.ArrayUtil;
import javax.net.ssl.SNIHostName;
import javax.net.ssl.SSLParameters;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Collections;
/**
* 其实就是copy了,cn.hutool.http.ssl.CustomProtocolsSSLFactory
*/
public class ExampleSSLFactory extends SSLSocketFactory {
private final String[] protocols;
private final SSLSocketFactory base;
public MeSSLFactory(String... protocols) throws IORuntimeException {
this.protocols = protocols;
this.base = SSLUtil.createSSLContext((String)null).getSocketFactory();
}
public String[] getDefaultCipherSuites() {
return this.base.getDefaultCipherSuites();
}
public String[] getSupportedCipherSuites() {
return this.base.getSupportedCipherSuites();
}
public Socket createSocket() throws IOException {
SSLSocket sslSocket = (SSLSocket)this.base.createSocket();
this.resetProtocols(sslSocket);
this.setSNIHostName(sslSocket);
return sslSocket;
}
public SSLSocket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException {
SSLSocket socket = (SSLSocket)this.base.createSocket(s, host, port, autoClose);
this.resetProtocols(socket);
this.setSNIHostName(socket);
return socket;
}
public Socket createSocket(String host, int port) throws IOException {
SSLSocket socket = (SSLSocket)this.base.createSocket(host, port);
this.resetProtocols(socket);
this.setSNIHostName(socket);
return socket;
}
public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException {
SSLSocket socket = (SSLSocket)this.base.createSocket(host, port, localHost, localPort);
this.resetProtocols(socket);
this.setSNIHostName(socket);
return socket;
}
public Socket createSocket(InetAddress host, int port) throws IOException {
SSLSocket socket = (SSLSocket)this.base.createSocket(host, port);
this.resetProtocols(socket);
this.setSNIHostName(socket);
return socket;
}
public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException {
SSLSocket socket = (SSLSocket)this.base.createSocket(address, port, localAddress, localPort);
this.resetProtocols(socket);
this.setSNIHostName(socket);
return socket;
}
private void resetProtocols(SSLSocket socket) {
if (ArrayUtil.isNotEmpty(this.protocols)) {
socket.setEnabledProtocols(this.protocols);
}
}
private void setSNIHostName(SSLSocket socket){
// 创建一个SSLParameters对象
SSLParameters sslParameters = new SSLParameters();
// 添加一个SNIHostName实例到SSLParameters对象中
sslParameters.setServerNames(Collections.singletonList(new SNIHostName("www.example.com")));
socket.setSSLParameters(sslParameters);
}
}