• 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();
        }
    }
  • 相关阅读:
    vue-cli项目npm run build后,index.html无法在浏览器打开
    前端工具
    关于 IIS 中 iconfont.woff 报 404(Not Found) 的原因即解决方法
    表格行上下移动、置顶的Javascript代码
    纯js国际化(使用技术:i18n)
    【整理】前端国际化小结
    js比较两个数字的大小不能直接比
    input事件中文触发多次问题研究
    linux(centos8):使用tree命令查看目录结构
    nginx安全:配置网站图片防盗链
  • 原文地址:https://www.cnblogs.com/adjk/p/11124345.html
Copyright © 2020-2023  润新知