zoukankan      html  css  js  c++  java
  • HttpsURLConnection信任任何证书

    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.security.SecureRandom;
    import java.security.cert.X509Certificate;
    import javax.net.ssl.HostnameVerifier;
    import javax.net.ssl.HttpsURLConnection;
    import javax.net.ssl.SSLContext;
    import javax.net.ssl.SSLSession;
    import javax.net.ssl.TrustManager;
    import javax.net.ssl.X509TrustManager;
    /**
     * Created by Leehom on 2017/11/27.
     */
    public class HttpsClient {
        private static final int CONNECT_TIME_OUT = 15000;
        private static final int READ_TIME_OUT = 15000;
        private static final TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }
            public void checkClientTrusted(X509Certificate[] certs, String authType) {
            }
            public void checkServerTrusted(X509Certificate[] certs, String authType) {
            }
        }};
        private static final HostnameVerifier NOT_VERYFY = new HostnameVerifier() {
            @Override
            public boolean verify(String s, SSLSession sslSession) {
                return true;
            }
        };
        /**
         * 发起http请求并获取结果
         *
         * @param spec   请求地址
         * @param method 请求方式(GET、POST)
         * @param data   提交的数据
         * @return JSONObject(通过JSONObject.get(key)的方式获取json对象的属性值)
         */
        public static HttpResult httpRequest(String spec, String method, String data) {
            try {
                URL url = new URL(spec);
                //如果地址是https开头,这里返回的HttpURLConnection实例其实是HttpURLConnection
                HttpURLConnection http = (HttpURLConnection) url.openConnection();
                http.setConnectTimeout(CONNECT_TIME_OUT);
                http.setReadTimeout(READ_TIME_OUT);
                http.setDoInput(true);
                if (data != null && data.length() > 0) {
                    http.setDoOutput(true);
                    http.setRequestMethod(method);
                    OutputStream out = http.getOutputStream();
                    out.write(data.toString().getBytes());
                    out.flush()
                }
                http.connect();
                int code = http.getResponseCode();
                if (code == 200) {
                    InputStream in = http.getInputStream();
                    byte[] buf = new byte[512];
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
                    int actual = in.read(buf);
                    while (actual != -1) {
                        bos.write(buf, 0, actual);
                        actual = in.read(buf);
                    }
                    in.close();
                    in = null;
                    bos.flush();
                    HttpResult result = new HttpResult(code, bos.toByteArray());
                    bos.close();
                    bos = null;
                    return result;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return new HttpResult();
        }
        /**
         * 发起https请求并获取结果
         *
         * @param spec   请求地址
         * @param method 请求方式(GET、POST)
         * @param data   提交的数据
         * @return JSONObject(通过JSONObject.get(key)的方式获取json对象的属性值)
         */
        public static HttpResult httpsRequest(String spec, String method, String data) {
            // Create a trust manager that does not validate certificate chains
            // Install the all-trusting trust manager
            try {// 注意这部分一定要
                HttpsURLConnection.setDefaultHostnameVerifier(NOT_VERYFY);
                SSLContext sc = SSLContext.getInstance("TLS");
                sc.init(null, trustAllCerts, new SecureRandom());
                HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
                return httpRequest(spec, method, data);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return new HttpResult();
        }
    }
  • 相关阅读:
    linux基础学习2
    linux下部署项目问题
    ThinkPHP上传返回 “文件上传保存错误!”
    jQuery自定义插件
    对于nginx为什么能提高性能
    WebSocket 是什么原理?为什么可以实现持久连接?
    数据库的左右外连接
    漫画说算法--动态规划算法一(绝对通俗易懂,非常棒)
    Integer.MIN_VALUE
    反射
  • 原文地址:https://www.cnblogs.com/adjk/p/11124345.html
Copyright © 2011-2022 走看看