微信红包接口-CA证书

package com.kaipai.ck.common;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.security.KeyStore;
import java.util.Properties;

import javax.net.ssl.SSLContext;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;

import com.kaipai.ck.wx.configs.WxConfigs;

@SuppressWarnings(“deprecation”)
public class AiwHttpsCAUtils {

private static final String CHARSET = "UTF-8";
//private static final String HTTP_POST = "POST";
//private static final String HTTP_GET = "GET";

/**
 * 方法
 * @param url  微信接口url
 * @param data xml格式
 * @return
 */
static public String doHttpsPost(String url, String data) {

    String result = null;
    try {
        String path = Thread.currentThread().getContextClassLoader().getResource("/").getPath();
        Properties props=System.getProperties(); //获得系统属性集    
        String osName = props.getProperty("os.name"); //操作系统名称  
        char oneChar = path.charAt(0);
        char twoChar = path.charAt(1);
        if (osName.startsWith("Win")) {
            if (oneChar=='/') {
                path = path.replaceFirst("/", "");
            }
        }else {
            if (oneChar=='/') {
                path = path.replaceFirst("/", "");
            }else if (twoChar=='/') {
                path = path.replaceFirst("/", "");
            }

        }
        path = path+WxConfigs.WxPay.WX_APICLIENT_CERT;

        KeyStore keyStore  = KeyStore.getInstance("PKCS12");
        FileInputStream instream = new FileInputStream(new File(path));
        try {
            keyStore.load(instream, WxConfigs.WxPay.MCH_ID.toCharArray());
        } finally {
            instream.close();
        }
        // Trust own CA and all self-signed certs
        SSLContext sslcontext = SSLContexts.custom()
                .loadKeyMaterial(keyStore, WxConfigs.WxPay.MCH_ID.toCharArray())
                .build();
        // Allow TLSv1 protocol only
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
                sslcontext,
                new String[] { "TLSv1" },
                null,
                SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
        CloseableHttpClient httpclient = HttpClients.custom()
                .setSSLSocketFactory(sslsf)
                .build();
        try {

            HttpPost httpPost = new HttpPost(url);
            System.out.println("executing request" + httpPost.getRequestLine());
            httpPost.setEntity(new StringEntity(data, CHARSET));
            CloseableHttpResponse response = httpclient.execute(httpPost);
            try {
                HttpEntity entity = response.getEntity();

                System.out.println("----------------------------------------");
                System.out.println(response.getStatusLine());
                if (entity != null) {
                    System.out.println("Response content length: " + entity.getContentLength());
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(entity.getContent()));
                    String text;
                    while ((text = bufferedReader.readLine()) != null) {
                        System.out.println(text);
                     //连接成一个字符串 
                            if (result != null)
                                result += text;
                            else
                                result = text;
                        }

                }
                EntityUtils.consume(entity);
            } finally {
                response.close();
            }
        } finally {
            httpclient.close();
        }
    } catch (Exception e) {
        // TODO: handle exception
    }
    return result;

}

}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值