package wechat;
import java.io.BufferedReader;
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.net.ConnectException;
import java.net.URL;
import java.security.KeyStore;
import java.util.List;
import java.util.TreeMap;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
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.conn.ssl.SSLContexts;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
/**
* 微信转账,http带签名请求
* @author ligp
* @date 2015-12-28
*/
@SuppressWarnings("deprecation")
public class HttpClientCustomSSL {
/**
* httpClient 带证书请求,该方法亲测使用,下面两种,一个是不同请求方式,另一个是不同参数封装方法,编码都有问题,没空测。
* @param parms
* @return 处理过的xml格式字符
* @throws Exception
*/
public static String httpClientResult(TreeMap<String, String> parms) throws Exception{
StringBuffer reultBuffer = new StringBuffer();
String requestXML = XMLUtil.getRequestXml(parms);// 封装好的请求XML
SSLConnectionSocketFactory sslsf = ReadSSl.getInstance().readCustomSSL();//获取证书
HttpPost httpPost = new HttpPost(WeChatConfig.POST_URL);
CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
StringEntity myEntity = new org.apache.http.entity.StringEntity(requestXML, WeChatConfig.CHARSET);
myEntity.setContentType("text/xml;charset=UTF-8");
myEntity.setContentEncoding(WeChatConfig.CHARSET);
httpPost.setHeader("Content-Type", "text/xml; charset=UTF-8");
httpPost.setEntity(myEntity);
CloseableHttpResponse response = null;
InputStream inputStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader bufferedReader = null;
try {
response = httpclient.execute(httpPost);
HttpEntity entity = response.getEntity();
if (entity!=null){
inputStream = entity.getContent();
inputStreamReader = new InputStreamReader(inputStream, WeChatConfig.CHARSET);
bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
while ((str = bufferedReader.readLine()) != null) {
reultBuffer.append(str);
}
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
// 释放资源
httpclient.close();
response.close();
bufferedReader.close();
inputStreamReader.close();
inputStream.close();
inputStream = null;
}
System.out.println("httpclient结果:"+reultBuffer.toString());
return reultBuffer.toString();
}
/**
* 第二种
*/
public static String httpsUrlRequest(String requestUrl, String outputStr) {
try {
KeyStore keyStore = KeyStore.getInstance("PKCS12");
FileInputStream instream = new FileInputStream(new File("D:/wechat/cert/apiclient_cert.p12"));
try {
keyStore.load(instream, WeChatConfig.MCHID.toCharArray());
} finally {
instream.close();
}
SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, WeChatConfig.MCHID.toCharArray()).build();
// SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory( sslcontext, new String[] { "TLSv1" }, null,
// SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
SSLSocketFactory ssf = sslcontext.getSocketFactory();
URL url = new URL(requestUrl);
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setSSLSocketFactory(ssf);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
// 设置请求方式(GET/POST)
conn.setRequestMethod("post");
conn.setRequestProperty("content-type", "application/x-www-form-urlencoded");
// 当outputStr不为null时向输出流写数据
if (null != outputStr) {
OutputStream outputStream = conn.getOutputStream();
// 注意编码格式
outputStream.write(outputStr.getBytes(WeChatConfig.CHARSET));
outputStream.close();
}
// 从输入流读取返回内容
InputStream inputStream = conn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, WeChatConfig.CHARSET);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
StringBuffer buffer = new StringBuffer();
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
// 释放资源
bufferedReader.close();
inputStreamReader.close();
inputStream.close();
inputStream = null;
conn.disconnect();
return buffer.toString();
} catch (ConnectException ce) {
} catch (Exception e) {
}
return null;
}
/**
* 第三种
* @throws Exception
*/
public void requestThree() throws Exception{
TreeMap<String, String> parms = new TreeMap<String, String>();
KeyStore keyStore = KeyStore.getInstance("PKCS12");
FileInputStream instream = new FileInputStream(new File("D:/wechat/cert/apiclient_cert.p12"));
try {
keyStore.load(instream, "1296637601".toCharArray());
} finally {
instream.close();
}
// Trust own CA and all self-signed certs
SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, "1296637601".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();
HttpPost httpPost = new HttpPost(WeChatConfig.POST_URL);
httpPost.setHeader("Content-Type", "text/xml; charset=UTF-8");
List<NameValuePair> nvps = WeChatUtil.generatListNameValuePair(parms);
UrlEncodedFormEntity uene = new UrlEncodedFormEntity(nvps,"utf-8");
uene.setContentType("text/xml;charset=UTF-8");
try {
httpPost.setEntity(uene);
CloseableHttpResponse response = httpclient.execute(httpPost);
HttpEntity entity = response.getEntity();
if (entity!=null){
InputStream inputStream = entity.getContent();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
StringBuffer buffer = new StringBuffer();
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
// 释放资源
bufferedReader.close();
inputStreamReader.close();
inputStream.close();
inputStream = null;
buffer.toString();
System.out.println("httpclient结果:"+buffer.toString());
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
WeChatPay.zip (32个子文件)
WeChatPay
.settings
org.eclipse.jpt.core.prefs 130B
org.eclipse.wst.common.project.facet.core.xml 221B
org.eclipse.core.resources.prefs 352B
org.eclipse.wst.common.project.facet.core.prefs.xml 167B
org.eclipse.jdt.core.prefs 364B
src
wechat
WeCahtPayController.java 3KB
SignTools.java 4KB
WeChatConfig.java 1003B
XMLParse.java 4KB
WeChatUtil.java 5KB
XMLUtil.java 3KB
HttpClientCustomSSL.java 8KB
ReadSSl.java 1KB
META-INF
persistence.xml 368B
.project 723B
.classpath 794B
build
httpclient-4.5.jar 711KB
xml-apis-1.0.b2.jar 107KB
servlet-api.jar 193KB
httpcore-4.4.1.jar 315KB
jdom-1.0.jar 150KB
classes
wechat
WeChatUtil.class 4KB
ReadSSl.class 2KB
WeCahtPayController.class 591B
HttpClientCustomSSL.class 8KB
WeChatConfig.class 830B
WeChatUtil$DateUtil.class 894B
XMLUtil.class 5KB
SignTools.class 4KB
XMLParse.class 5KB
META-INF
persistence.xml 368B
commons-codec-1.9.jar 258KB
共 32 条
- 1
你猜_猜猜猜
- 粉丝: 2
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页