hutool发送https请求,配置extensions的SNI

废话不多说直接上代码 

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);
    }
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Joomla extensions是指为Joomla网站平台开发的插件或扩展程序,用于增加网站的功能和特性。这些插件可以用于创建搜索组件和模块,以实现在Joomla数据库配置表列的搜索功能。可以创建两个表来存储搜索词和搜索结果的信息,通过记录所有搜索短语并将相应的结果存储到不同的表。同时,可以创建两个模块来显示先前的搜索结果,可以根据需求设置显示的结果数量,并通过SEF方式显示。这些扩展程序可以通过Joomla平台进行配置,具体的配置信息将在之后发送给使用者。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Joomla extensions coding Search for somebody with much experience of Joomla. $10000](https://blog.csdn.net/huangweijian888/article/details/2348329)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Joomla-Questions-and-Answers:问题和答案](https://download.csdn.net/download/weixin_42181888/18985151)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值