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;
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
weixinpay.zip (55个子文件)
weixinpay
.project 1KB
pom.xml 5KB
target
test-classes
m2e-wtp
web-resources
META-INF
maven
com.weixinpay
weixinpay
pom.xml 2KB
pom.properties 242B
MANIFEST.MF 112B
m2e-jee
web-resources
META-INF
maven
com.weixinpay
weixinpay
pom.xml 5KB
pom.properties 217B
MANIFEST.MF 114B
classes
log4j.properties 561B
com
weixinpay
Sign.class 1KB
GetOpenId.class 1015B
model
SignInfo.class 1KB
OrderInfo.class 3KB
OrderReturnInfo.class 2KB
PayResult.class 1KB
common
StreamUtil.class 1020B
Configure.class 1KB
RandomStringGenerator.class 1019B
HttpRequest$MyX509TrustManager.class 997B
HttpRequest.class 3KB
Signature.class 2KB
MD5.class 2KB
.DS_Store 6KB
Xiadan.class 1KB
.DS_Store 6KB
.DS_Store 6KB
.settings
.jsdtscope 626B
org.eclipse.wst.common.component 754B
org.eclipse.wst.validation.prefs 48B
org.eclipse.jdt.core.prefs 422B
org.eclipse.wst.common.project.facet.core.xml 245B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.m2e.core.prefs 86B
org.eclipse.wst.jsdt.ui.superType.name 6B
src
test
java
main
resources
log4j.properties 561B
java
com
weixinpay
Sign.java 2KB
Xiadan.java 3KB
PayResult.java 2KB
model
OrderInfo.java 2KB
OrderReturnInfo.java 1KB
SignInfo.java 1008B
GetOpenId.java 2KB
common
StreamUtil.java 541B
Configure.java 804B
HttpRequest.java 4KB
RandomStringGenerator.java 693B
MD5.java 1KB
Signature.java 2KB
.DS_Store 6KB
.DS_Store 6KB
webapp
WEB-INF
web.xml 1KB
.DS_Store 6KB
.DS_Store 6KB
.classpath 1KB
.DS_Store 6KB
共 55 条
- 1
资源评论
星空下的风
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功