zoukankan      html  css  js  c++  java
  • 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;
        }
    }
  • 相关阅读:
    三大主流负载均衡软件对比(LVS+Nginx+HAproxy)
    nginx 提示the "ssl" directive is deprecated, use the "listen ... ssl" directive instead
    centos安装nginx并配置SSL证书
    hadoop创建目录文件失败
    The server time zone value 'EDT' is unrecognized or represents more than one time zone.
    脚本启动SpringBoot(jar)
    centos做免密登录
    数据库远程连接配置
    Bash 快捷键
    TCP三次握手四次断开
  • 原文地址:https://www.cnblogs.com/liran123/p/14759372.html
Copyright © 2011-2022 走看看