最近自己搞微信支付开发时遇到了些问题,其中一个就是关于微信中需要发起HTTPS请求,下面贴代码,其中Config.getMch_id()方法是获取微信分给商户的一个ID,Config.getLicense_path()方法是获取证书的路径,这两个参数可以自己替换
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.security.KeyStore;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.net.ssl.SSLContext;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContexts;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import com.weixin.information.Config;
public class HttpUtil implements Http {
public InputStream httpsPost(String url, String content, String charset) {
return https(url, content, null, charset, "post");
}
public InputStream httpsPost(String url, Map<String, String> content,
String charset) {
return https(url, null, content, charset, "post");
}
public InputStream httpsGet(String url) {
return https(url, null, null, null, "get");
}
/**
* 发起https请求
*
* @param url
* @param content
* @param mapContent
* @param charset
* @param method
* @return
*/
private static InputStream https(String url, String content,
Map<String, String> mapContent, String charset, String method) {
try {
CloseableHttpClient httpclient = getHttpClient();
CloseableHttpResponse response = null;
InputStream is = null;
try {
// 判断请求方式是post还是get
if (method.equals("post")) {
HttpPost httpPost = new HttpPost(url);
//判断post的数据是String类型还是Map类型
if (content != null) {
HttpEntity httpEntity = new StringEntity(content,
charset);
httpPost.setEntity(httpEntity);
} else if (mapContent != null) {
List<NameValuePair> list = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> entry : mapContent
.entrySet()) {
list.add(new BasicNameValuePair(entry.getKey(),
entry.getValue()));
}
httpPost.setEntity(new UrlEncodedFormEntity(list,
charset));
}
response = httpclient.execute(httpPost);
} else if (method.equals("get")) {
HttpGet httpget = new HttpGet(url);
// 获取response
response = httpclient.execute(httpget);
}
// 从response中获取响应实体
HttpEntity entity = response.getEntity();
// 获取响应实体对应的响应内容的输入流
is = entity != null ? entity.getContent() : null;
// 销毁entity
EntityUtils.consume(entity);
return is;
} finally {
httpclient.close();
response.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 获取HttpClient
*
* @return CloseableHttpClient对象,
*/
private static CloseableHttpClient getHttpClient() {
try {
FileInputStream fis = null;
CloseableHttpClient httpclient = null;
try {
// 指定证书格式为PKCS12
KeyStore keyStore = KeyStore.getInstance("PKCS12");
// 读取证书(该证书从微信公众平台下载)
File file = new File(Config.getLicense_path());
fis = new FileInputStream(file);
// 指定PKCS12的密码(商户ID)
keyStore.load(fis, Config.getMch_id().toCharArray());
// Trust own CA and all self-signed certs
SSLContext sslcontext = SSLContexts
.custom()
.loadKeyMaterial(keyStore,
Config.getMch_id().toCharArray()).build();
// 指定TLS版本
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
sslcontext,
new String[] { "TLSv1" },
null,
SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
// 设置httpclient的SSLSocketFactory
httpclient = HttpClients.custom().setSSLSocketFactory(sslsf)
.build();
return httpclient;
} finally {
fis.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}