package com.weixinpay.common;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import javax.net.ssl.SSLContext;
import javax.net.ssl.X509TrustManager;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
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.HttpClients;
import org.apache.http.util.EntityUtils;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.thoughtworks.xstream.io.xml.XmlFriendlyNameCoder;
public class HttpRequest {
//连接超时时间,默认10秒
private static final int socketTimeout = 10000;
//传输超时时间,默认30秒
private static final int connectTimeout = 30000;
/**
* post请求
* @throws IOException
* @throws ClientProtocolException
* @throws NoSuchAlgorithmException
* @throws KeyStoreException
* @throws KeyManagementException
* @throws UnrecoverableKeyException
*/
public static String sendPost(String url, Object xmlObj) throws ClientProtocolException, IOException, UnrecoverableKeyException, KeyManagementException, KeyStoreException, NoSuchAlgorithmException {
HttpPost httpPost = new HttpPost(url);
//解决XStream对出现双下划线的bug
XStream xStreamForRequestPostData = new XStream(new DomDriver("UTF-8", new XmlFriendlyNameCoder("-_", "_")));
xStreamForRequestPostData.alias("xml", xmlObj.getClass());
//将要提交给API的数据对象转换成XML格式数据Post给API
String postDataXML = xStreamForRequestPostData.toXML(xmlObj);
//得指明使用UTF-8编码,否则到API服务器XML的中文不能被成功识别
StringEntity postEntity = new StringEntity(postDataXML, "UTF-8");
httpPost.addHeader("Content-Type", "text/xml");
httpPost.setEntity(postEntity);
//设置请求器的配置
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(socketTimeout).setConnectTimeout(connectTimeout).build();
httpPost.setConfig(requestConfig);
HttpClient httpClient = HttpClients.createDefault();
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity, "UTF-8");
return result;
}
/**
* 自定义证书管理器,信任所有证书
* @author pc
*
*/
public static class MyX509TrustManager implements X509TrustManager {
@Override
public void checkClientTrusted(
java.security.cert.X509Certificate[] arg0, String arg1)
throws java.security.cert.CertificateException {
// TODO Auto-generated method stub
}
@Override
public void checkServerTrusted(
java.security.cert.X509Certificate[] arg0, String arg1)
throws java.security.cert.CertificateException {
// TODO Auto-generated method stub
}
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
// TODO Auto-generated method stub
return null;
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
小程序微信支付.zip (66个子文件)
小程序微信支付
.DS_Store 6KB
小程序端
weixin_pay
app.js 719B
utils
util.js 460B
pages
index
index.wxml 95B
index.js 2KB
index.wxss 268B
logs
logs.wxml 194B
logs.js 266B
logs.wxss 106B
logs.json 54B
app.json 241B
app.wxss 194B
服务端java
weixinpay
src
.DS_Store 6KB
main
resources
log4j.properties 561B
.DS_Store 6KB
java
com
weixinpay
model
SignInfo.java 1008B
OrderInfo.java 2KB
OrderReturnInfo.java 1KB
GetOpenId.java 2KB
.DS_Store 6KB
Sign.java 2KB
common
StreamUtil.java 541B
Configure.java 837B
Signature.java 2KB
MD5.java 1KB
HttpRequest.java 4KB
RandomStringGenerator.java 693B
PayResult.java 2KB
Xiadan.java 3KB
.DS_Store 6KB
.DS_Store 6KB
webapp
WEB-INF
web.xml 1KB
.classpath 1KB
.DS_Store 6KB
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.common.project.facet.core.xml 245B
org.eclipse.m2e.core.prefs 86B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.component 658B
.jsdtscope 626B
org.eclipse.jdt.core.prefs 422B
org.eclipse.wst.validation.prefs 48B
pom.xml 2KB
target
classes
com
weixinpay
GetOpenId.class 2KB
model
OrderInfo.class 3KB
SignInfo.class 1KB
OrderReturnInfo.class 2KB
.DS_Store 6KB
PayResult.class 2KB
Xiadan.class 4KB
common
StreamUtil.class 1020B
HttpRequest$MyX509TrustManager.class 997B
MD5.class 2KB
Signature.class 4KB
Configure.class 1KB
RandomStringGenerator.class 1019B
HttpRequest.class 3KB
Sign.class 3KB
.DS_Store 6KB
.DS_Store 6KB
log4j.properties 561B
test-classes
m2e-wtp
web-resources
META-INF
MANIFEST.MF 112B
maven
com.weixinpay
weixinpay
pom.properties 242B
pom.xml 2KB
.project 1KB
.DS_Store 6KB
共 66 条
- 1
资源评论
changhaibin
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功