【java微信开发】红包接口调用

//import相关类
import java.io.FileInputStream;
import java.security.KeyStore;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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.util.EntityUtils;
import org.springframework.web.context.ContextLoader;


//加载密钥
KeyStore clientKeyStore = KeyStore.getInstance("PKCS12");
clientKeyStore.load(new FileInputStream(<!--微信支付私有证书-->, <!--微信支付分配的商户号-->);
//加载密钥 
KeyStore clientTrustKeyStore = KeyStore.getInstance("PKCS12");  
clientTrustKeyStore.load(new FileInputStream(<!--微信支付私有证书-->, <!--微信支付分配的商户号-->);
//密钥工厂
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());  
kmf.init(clientKeyStore, <!--微信支付分配的商户号-->); 
//通道信任 
TrustManager[] tm = { new MyX509TrustManager() }; 
//通道协议
SSLContext sslContext = SSLContext.getInstance("TLSv1");  
sslContext.init(kmf.getKeyManagers(), tm, new java.security.SecureRandom());
//通道工厂
SSLSocketFactory socketFactory = sslContext.getSocketFactory();
//创建通道端口
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(socketFactory, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
//开始调用
HttpPost httppost = new HttpPost(<!--调用地址-->);
httppost.addHeader("Connection", "keep-alive");
httppost.addHeader("Accept", "*/*");
httppost.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
httppost.addHeader("Host", "api.mch.weixin.qq.com");
httppost.addHeader("X-Requested-With", "XMLHttpRequest");
httppost.addHeader("Cache-Control", "max-age=0");
httppost.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0) ");
httppost.setEntity(new StringEntity(<!--xml-->, "utf-8"));
//调用
CloseableHttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
//获取返回的字符串
String result = new String(EntityUtils.toString(entity,"gbk").getBytes(),"utf-8");



//此处用到了一个类
import java.security.cert.CertificateException;  
import java.security.cert.X509Certificate;
import javax.net.ssl.X509TrustManager; 
public class MyX509TrustManager implements X509TrustManager {  
    public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {}  
  
    public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {}  
  
    public X509Certificate[] getAcceptedIssuers() {  
        return null;  
    }  
}


你可能感兴趣的:(java,证书错误,微信开发,微信红包,红包接口)