当把要发送的数据准备好之后,接下来是要写https请求,把它发送给微信服务器
String reback = HttpClientUtil.sendHttpsUrl("https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack", data);
需要抓取异常,并处理好异常
写一个HttpClientUtil类
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.UnrecoverableKeyException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.Map;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManagerFactory;
import com.yxht.core.common.tools.LoadProperties;
import com.yxht.core.modules.WechatPay.client.TenpayHttpClient;
/**
* Http客户端工具类<br/>
* 这是内部调用类,请不要在外部调用。
* @author miklchen
*
*/
public class HttpClientUtil {
/**
* 发送请求(微信支付用,发红包等等)
* @param url
* @param params
* @return
* @throws Exception
*/
public static String sendHttpsUrl(String sendUrl, String params) throws Exception{
String jsonStr = "";
String certificate_path = "d\:\\rootca.pem"; //信任库证书
String keystore_path = "d\:\\apiclient_cert.p12"; //密钥库证书
String password = "xxxxxxxx"; //商户号,也是加载证书的密码
// 声明SSL上下文
SSLContext sslContext = null;
// 实例化主机名验证接口
sslContext = getSSLContext(password, keystore_path, certificate_path);
if (sslContext != null) {
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext
.getSocketFactory());
}
URL url = new URL(sendUrl);
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
con.setDoInput(true);
con.setUseCaches(false);
//设置套接工厂
con.setSSLSocketFactory(sslContext.getSocketFactory());
OutputStreamWriter writer = new OutputStreamWriter(
con.getOutputStream(), "UTF-8");
writer.write(params);
writer.flush();
writer.close();
InputStream in = con.getInputStream();
BufferedReader db = new BufferedReader(new InputStreamReader(in, "UTF-8"));
String tmp = "";
while ((tmp = db.readLine()) != null) {
jsonStr += tmp;
}
return jsonStr;
}
/**
* 获得SSLSocketFactory.
* @param password
* 密码
* @param keyStorePath
* 密钥库路径
* @param trustStorePath
* 信任库路径
* @return SSLSocketFactory
* @throws Exception
*/
public static SSLContext getSSLContext(String password,
String keyStorePath, String trustStorePath) throws Exception {
// 实例化密钥库
KeyManagerFactory keyManagerFactory = KeyManagerFactory
.getInstance(KeyManagerFactory.getDefaultAlgorithm());
// 获得密钥库
KeyStore keyStore = getKeyStore(password, keyStorePath);
// 初始化密钥工厂
keyManagerFactory.init(keyStore, password.toCharArray());
// 实例化信任库
// TrustManagerFactory trustManagerFactory = TrustManagerFactory
// .getInstance(TrustManagerFactory.getDefaultAlgorithm());
// // 获得信任库
// KeyStore trustStore = getKeyStore(password, trustStorePath);
// // 初始化信任库
// trustManagerFactory.init(trustStore);
// 实例化SSL上下文
SSLContext ctx = SSLContext.getInstance("TLS");
// 初始化SSL上下文
ctx.init(keyManagerFactory.getKeyManagers(),
null, null);
// 获得SSLSocketFactory
return ctx;
}
/**
* 获得KeyStore.
* @param keyStorePath
* 密钥库路径
* @param password
* 密码
* @return 密钥库
* @throws Exception
*/
public static KeyStore getKeyStore(String password, String keyStorePath)
throws Exception {
// 实例化密钥库
KeyStore ks = KeyStore.getInstance("PKCS12");
// 获得密钥库文件流
FileInputStream is = new FileInputStream(keyStorePath);
// 加载密钥库
ks.load(is, password.toCharArray());
// 关闭密钥库文件流
is.close();
return ks;
}
}
有些import的没用,可以删掉,只要程序代码没有报错的地方就行。
到这里,如果发送请求,会失败,它会说你没有安装证书,到商户平台上去下载证书,文档里有说明
下载后一共有4个证书,至少需要密钥库证书apiclient_cert.p12,因为信任库证书rootca.pem我不会用(哪位大神会用教教我),
文档里也说了信任库证书不用也行,证书安装请仔细研究文档,代码上面已经写好了。
最后最后,,请发送请求吧!!!
声明:工具类代码参考微信支付开发的demo,有些自己做了小改动。
PS:需要引入http相关jar包,httpcore-4.2.3.jar,httpmine-4.2.3.jar,httpclient-4.2.3.jar,httpclient-cache-4.2.3.jar。一样,
还是去https://repository.sonatype.org里面找吧,如果是maven建立的项目,就把他们引入到pom.xml里面去吧!