NutzCN Logo
分享 使用http工具访问微信带证书的https
发布于 2589天前 作者 happydaygtr 2166 次浏览 复制 上一个帖子 下一个帖子
标签:
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

引入包

---------------------------


----------------------------
private static Header header = Header.create();

 /**
     * post请求
     * @param url(请求url)
     * @param context (请求内容)
     * @param isSSL (是否使用https)
     * @return
     */
    private static String post(String url,String context,boolean isSSL){
        String  responseString="";
        //https请求
        if(isSSL){
            setSSLSocketFactory();
        }
      
        //发起请求
       Response response = Http.post3(url, context, header, 10*1000);
       responseString   = response.getContent();
       return responseString;
    }
    
    /**
     * 设置HttpSSL请求
     * @param path (微信证书库xx_cert.p12 路径)
     * @param password (证书密码)
     */
    private static void setSSLSocketFactory(String path,String password){
        SSLContext sslContext = null;
        
        try {
            KeyStore keyStore  = KeyStore.getInstance("PKCS12");
            FileInputStream instream = new FileInputStream(new File(path));
            try {
                keyStore.load(instream, password.toCharArray());
            } finally {
                instream.close();
            }

//        InputStream is = new FileInputStream("cert.crt");
//        CertificateFactory cf = CertificateFactory.getInstance("X.509");
//        X509Certificate caCert = (X509Certificate)cf.generateCertificate(is);
//        KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
//        ks.load(null); // You don't need the KeyStore instance to come from a file.
//        ks.setCertificateEntry("caCert", caCert);
//            TrustManagerFactory tmf = TrustManagerFactory
//                .getInstance(TrustManagerFactory.getDefaultAlgorithm());
//            tmf.init(keyStore);

            //证书密码库
            KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
            kmf.init(keyStore, password.toCharArray());
            
            // 证书
            TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return new java.security.cert.X509Certificate[] {};
                }
                public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                }
                public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                }
            } };
            //连接上下文(微信需要TLSv1协议)
            sslContext = SSLContext.getInstance("TLSv1");
            sslContext.init(kmf.getKeyManagers(), trustAllCerts, new SecureRandom());
            
            Http.disableJvmHttpsCheck();
            Http.setSSLSocketFactory(sslContext.getSocketFactory());
        } catch (KeyManagementException e1) {
        } catch (KeyStoreException e1) {
        } catch (FileNotFoundException e1) {
        } catch (NoSuchAlgorithmException e1) {
        } catch (UnrecoverableKeyException e) {
        }catch (CertificateException e1) {
        } catch (IOException e1) {
        }
    }
0 回复
添加回复
请先登陆
回到顶部