发送HTTPS请求

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了发送HTTPS请求相关的知识,希望对你有一定的参考价值。

package com.suning.epp.trt.util.r32epp;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.URLDecoder;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.List;
import javax.net.ssl.SSLContext;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
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.message.BasicNameValuePair;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.TrustStrategy;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class HttpsClientUtil {
    private static final String UTF_8 = "UTF-8";
    private HttpsClientUtil() {
    }
    private static final Logger log = LoggerFactory
            .getLogger(HttpsClientUtil.class);
    public static String post(List<BasicNameValuePair> params, String url,
            String logPrefix) {
 
        CloseableHttpClient httpClient = createSSLClientDefault(logPrefix);
        try {
            HttpPost post = createPost(params, url, logPrefix);
            HttpResponse response = httpClient.execute(post);
            HttpEntity entity = response.getEntity();
            String body = EntityUtils.toString(entity);
            return body;
        } catch (Exception e) {
            throw new EppRuntimeException(e);
        } finally {
            try {
                httpClient.close();
            } catch (IOException e) {
                log.error(logPrefix, e);
            }
        }
    }
    public static CloseableHttpClient createSSLClientDefault(String logPrefix) {
        try {
            SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(
                    nullnew TrustStrategy() {
                        // 信任所有
                        public boolean isTrusted(X509Certificate[] chain,
                                String authType) throws CertificateException {
                            return true;
                        }
                    }).build();
            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
                    sslContext);
            return HttpClients.custom().setSSLSocketFactory(sslsf).build();
        } catch (Exception e) {
            log.error(logPrefix, e);
        }
        return HttpClients.createDefault();
    }
    private static HttpPost createPost(List<BasicNameValuePair> params,
            String url, String logPrefix) throws Exception {
        HttpPost post = new HttpPost(url);
        HttpEntity httpEntity = new UrlEncodedFormEntity(params, UTF_8);
        post.setEntity(httpEntity);
        printParams(url, logPrefix, httpEntity);
        return post;
    }
    private static void printParams(String url, String logPrefix,
            HttpEntity httpEntity) throws IOException {
        BufferedInputStream bif = null;
        try {
            bif = new BufferedInputStream(httpEntity.getContent());
            byte[] byteArr = new byte[(int) httpEntity.getContentLength()];
            byte tmpByte[] = new byte[1024];
            int totalCounts = 0;
            int size = bif.read(tmpByte);
            while (size > 0) {
                System.arraycopy(tmpByte, 0, byteArr, totalCounts, size);
                totalCounts = totalCounts + size;
                size = bif.read(tmpByte);
            }
            log.info(logPrefix + "HTTP请求:{}?{}", url,
                    URLDecoder.decode(new String(byteArr, UTF_8), UTF_8));
        } catch (Exception e) {
            log.error(logPrefix, e);
        } finally {
            if (bif != null) {
                bif.close();
            }
        }
    }
}

以上是关于发送HTTPS请求的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 WinInet 发送 HTTPS 请求?

接入天猫精灵auth2授权页面https发送ajax请求

Jmeter发送HTTPS请求

通过 Apache Commons HttpClient 发送 HTTPS 请求

使用码头 Http 客户端发送 Https 请求中的 NullPointerException

nodejs中发送https请求怎么设置请求头

(c)2006-2024 SYSTEM All Rights Reserved IT常识