HTTPClint调用https请求工具类(springboot中的)

使用httpclint做https请求的小工具

需要导入pom的

<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents.client5/httpclient5 -->
		<dependency>
			<groupId>org.apache.httpcomponents.client5</groupId>
			<artifactId>httpclient5</artifactId>
			<version>5.2.1</version>
		</dependency>

import io.renren.modules.app.entity.Customizeconfig;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.net.ssl.SSLContext;
import java.io.File;
import java.io.FileInputStream;
import java.security.KeyStore;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;

/**
 * @author xiaosi
 * @date 2023/12/4
 */
@Component
public class HttpclintUtil {
    @Autowired
    private Customizeconfig customizeconfig;
    public HttpResponse httpsclint(HttpRequestBase requestBase) throws Exception {
        // 导入公钥文件
        File file = new File(customizeconfig.keyPath);
        FileInputStream instream = new FileInputStream(file);
        Certificate cert = CertificateFactory.getInstance(customizeconfig.keyType).generateCertificate(instream);

        try {
            // 创建 KeyStore 并加载公钥
            KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
            trustStore.load(null, null);
            trustStore.setCertificateEntry("alias",cert);

            // 创建 SSL 上下文
            SSLContext sslContext = SSLContexts.custom()
                    .loadTrustMaterial(trustStore, null)
                    .build();

            // 创建 SSL 连接工厂
            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, new NoopHostnameVerifier());

            // 创建 HttpClient
            CloseableHttpClient httpclient = HttpClients.custom()
                    .setSSLSocketFactory(sslsf)
                    .build();

            // 创建 HttpGet 请求对象
//            HttpGet httpGet = new HttpGet("https://api.example.com/resource");

            // 设置请求超时时间和连接超时时间
            RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(5000).setSocketTimeout(5000).build();
            requestBase.setConfig(requestConfig);

            // 发送请求并获取响应
            HttpResponse response = httpclient.execute(requestBase);
            httpclient.close();
            return response ;
        } finally {
            instream.close();
        }
    }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

什么都不会的小肆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值