【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;  
    }  
}


转载于:https://my.oschina.net/zhuxuan/blog/397457

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值