Spring RestTemplate调用https,Hutool工具类调用https

Spring RestTemplate调用https (需要有配置信任证书。下一篇博客配置springboot https )

Hutool工具类调用https(默认会携带几个头信息,不需要的话可以去掉)

 

package cn.luischen.controller;

import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
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.SSLContextBuilder;
import org.apache.http.ssl.TrustStrategy;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import javax.net.ssl.SSLContext;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Arrays;
import java.util.Map;

public class TestHttp {
    /**
     * hutools 调用https
     *
     * @param url
     * @param params
     * @return
     */

    public JSONObject send(String url, Map<String, Object> params) {
        HttpRequest request = HttpRequest.post(url).body(JSONObject.toJSONString(params));
        request.removeHeader(Header.USER_AGENT);
        String body = request.execute().body();
        return JSONObject.parseObject(body);
    }

    /**
     * Spring RestTemplate调用https
     *
     * @param url
     * @param params
     * @return
     */
    public JSONObject send2(String url, Map<String, Object> params) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.setAccept(Arrays.asList(MediaType.ALL));
        HttpEntity<Object> entity = new HttpEntity<>(JSONObject.toJSONString(params), headers);
        try {
            String body = getRestTemplate().postForObject(url, entity, String.class);
            return JSONObject.parseObject(body);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public static RestTemplate getRestTemplate() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
        SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
            @Override
            public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
                return true;
            }
        }).build();

        SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext,
                new String[]{"TLSv1"},
                null,
                NoopHostnameVerifier.INSTANCE);

        CloseableHttpClient httpClient = HttpClients.custom()
                .setSSLSocketFactory(csf)
                .build();

        HttpComponentsClientHttpRequestFactory requestFactory =
                new HttpComponentsClientHttpRequestFactory();

        requestFactory.setHttpClient(httpClient);
        RestTemplate restTemplate = new RestTemplate(requestFactory);
        return restTemplate;
    }
}

 

posted @ 2021-05-12 13:23  _Phoenix  阅读(2412)  评论(0编辑  收藏  举报