zoukankan      html  css  js  c++  java
  • 发送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();
                }
            }
        }
    }
  • 相关阅读:
    Revit 二次开发 交互及UIAPI之TaskDialog
    Revit 二次开发 交互及UIAPI之Selection
    Revit 二次开发 元素创建与修改练习
    编译带libev和libuv的libwebsocket (Win平台)
    sqlite3存储格式
    MAC OS下编译apple跨平台的libevent库 (可延申到其它第三库)
    Unix, Linux以及NT内核和它们各自衍生的系统关系图
    简单地迁移你的android jni代码逻辑到iOS
    使用ndk交叉编译android各平台版本的第三方库
    使用Android Studio进行ndk开发的准备
  • 原文地址:https://www.cnblogs.com/jiangzhengjun/p/7542476.html
Copyright © 2011-2022 走看看