package com.weixinpay.util;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
/**
* 财付通http或者https网络通信客户端<br/>
* ========================================================================<br/>
* api说明:<br/>
* setReqContent($reqContent),设置请求内容,无论post和get,都用get方式提供<br/>
* getResContent(), 获取应答内容<br/>
* setMethod(method),设置请求方法,post或者get<br/>
* getErrInfo(),获取错误信息<br/>
* setCertInfo(certFile, certPasswd),设置证书,双向https时需要使用<br/>
* setCaInfo(caFile), 设置CA,格式未pem,不设置则不检查<br/>
* setTimeOut(timeOut), 设置超时时间,单位秒<br/>
* getResponseCode(), 取返回的http状态码<br/>
* call(),真正调用接口<br/>
* getCharset()/setCharset(),字符集编码<br/>
*
* ========================================================================<br/>
*
*/
public class TenpayHttpClient {
private static final String USER_AGENT_VALUE =
"Mozilla/4.0 (compatible; MSIE 6.0; Windows XP)";
private static final String JKS_CA_FILENAME =
"tenpay_cacert.jks";
private static final String JKS_CA_ALIAS = "tenpay";
private static final String JKS_CA_PASSWORD = "";
/** ca证书文件 */
private File caFile;
/** 证书文件 */
private File certFile;
/** 证书密码 */
private String certPasswd;
/** 请求内容,无论post和get,都用get方式提供 */
private String reqContent;
/** 应答内容 */
private String resContent;
/** 请求方法 */
private String method;
/** 错误信息 */
private String errInfo;
/** 超时时间,以秒为单位 */
private int timeOut;
/** http应答编码 */
private int responseCode;
/** 字符编码 */
private String charset;
private InputStream inputStream;
public TenpayHttpClient() {
this.caFile = null;
this.certFile = null;
this.certPasswd = "";
this.reqContent = "";
this.resContent = "";
this.method = "POST";
this.errInfo = "";
this.timeOut = 30;//30秒
this.responseCode = 0;
this.charset = "GBK";
this.inputStream = null;
}
/**
* 设置证书信息
* @param certFile 证书文件
* @param certPasswd 证书密码
*/
public void setCertInfo(File certFile, String certPasswd) {
this.certFile = certFile;
this.certPasswd = certPasswd;
}
/**
* 设置ca
* @param caFile
*/
public void setCaInfo(File caFile) {
this.caFile = caFile;
}
/**
* 设置请求内容
* @param reqContent 表求内容
*/
public void setReqContent(String reqContent) {
this.reqContent = reqContent;
}
/**
* 获取结果内容
* @return String
* @throws IOException
*/
public String getResContent() {
try {
this.doResponse();
} catch (IOException e) {
this.errInfo = e.getMessage();
//return "";
}
return this.resContent;
}
/**
* 设置请求方法post或者get
* @param method 请求方法post/get
*/
public void setMethod(String method) {
this.method = method;
}
/**
* 获取错误信息
* @return String
*/
public String getErrInfo() {
return this.errInfo;
}
/**
* 设置超时时间,以秒为单位
* @param timeOut 超时时间,以秒为单位
*/
public void setTimeOut(int timeOut) {
this.timeOut = timeOut;
}
/**
* 获取http状态码
* @return int
*/
public int getResponseCode() {
return this.responseCode;
}
/**
* 执行http调用。true:成功 false:失败
* @return boolean
*/
public boolean call() {
boolean isRet = false;
//http
if(null == this.caFile && null == this.certFile) {
try {
this.callHttp();
isRet = true;
} catch (IOException e) {
this.errInfo = e.getMessage();
}
return isRet;
}
//https
try {
this.callHttps();
isRet = true;
} catch (UnrecoverableKeyException e) {
this.errInfo = e.getMessage();
} catch (KeyManagementException e) {
this.errInfo = e.getMessage();
} catch (CertificateException e) {
this.errInfo = e.getMessage();
} catch (KeyStoreException e) {
this.errInfo = e.getMessage();
} catch (NoSuchAlgorithmException e) {
this.errInfo = e.getMessage();
} catch (IOException e) {
this.errInfo = e.getMessage();
}
return isRet;
}
protected void callHttp() throws IOException {
if("POST".equals(this.method.toUpperCase())) {
String url = HttpClientUtil.getURL(this.reqContent);
String queryString = HttpClientUtil.getQueryString(this.reqContent);
byte[] postData = queryString.getBytes(this.charset);
this.httpPostMethod(url, postData);
return ;
}
this.httpGetMethod(this.reqContent);
}
protected void callHttps() throws IOException, CertificateException,
KeyStoreException, NoSuchAlgorithmException,
UnrecoverableKeyException, KeyManagementException {
// ca目录
String caPath = this.caFile.getParent();
File jksCAFile = new File(caPath + "/"
+ TenpayHttpClient.JKS_CA_FILENAME);
if (!jksCAFile.isFile()) {
X509Certificate cert = (X509Certificate) HttpClientUtil
.getCertificate(this.caFile);
FileOutputStream out = new FileOutputStream(jksCAFile);
// store jks file
HttpClientUtil.storeCACert(cert, TenpayHttpClient.JKS_CA_ALIAS,
TenpayHttpClient.JKS_CA_PASSWORD, out);
out.close();
}
FileInputStream trustStream = new FileInputStream(jksCAFile);
FileInputStream keyStream = new FileInputStream(this.certFile);
SSLContext sslContext = HttpClientUtil.getSSLContext(trustStream,
TenpayHttpClient.JKS_CA_PASSWORD, keyStream, this.certPasswd);
//关闭流
keyStream.close();
trustStream.close();
if("POST".equals(this.method.toUpperCase())) {
String url = HttpClientUtil.getURL(this.reqContent);
String queryString = HttpClientUtil.getQueryString(this.reqContent);
byte[] postData = queryString.getBytes(this.charset);
this.httpsPostMethod(url, postData, sslContext);
return ;
}
this.httpsGetMethod(this.reqContent, sslContext);
}
public boolean callHttpPost(String url, String postdata) {
boolean flag = false;
byte[] postData;
try {
postData = postdata.getBytes(this.charset);
this.httpPostMethod(url, postData);
flag = true;
} catch (IOException e1) {
e1.printStackTrace();
}
return flag;
}
/**
* 以http post方式通信
* @param url
* @param postData
* @throws IOException
*/
protected void httpPostMethod(String url, byte[] postData)
throws IOException {
HttpURLConnection conn = HttpClientUtil.getHttpURLConnection(url);
this.doPost(conn, postData);
}
/**
* 以http get方式通信
*
* @param url
* @throws IOException
*/
protected void httpGetMethod(String url) throws IOException {
HttpURLConnection httpConnection =
HttpClientUtil.getHttpURLConnection(url);
this.setHttpRequest(httpConnection);
httpConnection.setRequestMethod("GET");
this.responseCode = httpConnection.getResponseCode();
this.inputStream = httpConnection.getInputStream();
}
/**
* 以https get方式通信
* @param url
* @param sslContext
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
weixinpay.zip (21个子文件)
weixinpay
.project 2KB
.mymetadata 300B
src
com
weixinpay
util
TenpayHttpClient.java 10KB
HttpClientUtil.java 8KB
WeiXinPay.java 4KB
WebRoot
WEB-INF
classes
com
weixinpay
WeiXinPay.class 5KB
util
TenpayHttpClient.class 8KB
HttpClientUtil.class 7KB
lib
commons-httpclient-3.1.jar 298KB
commons-codec-1.10.jar 278KB
web.xml 404B
index.jsp 837B
META-INF
MANIFEST.MF 36B
.myeclipse
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.common.project.facet.core.xml 252B
org.eclipse.jdt.core.prefs 364B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.core.resources.prefs 234B
org.eclipse.wst.common.component 465B
.jsdtscope 500B
.classpath 529B
共 21 条
- 1
资源评论
- wjhjsj2018-07-14不知道怎么用,运行不了
- 迈向前景2020-02-17不知道怎么用,运行不了
- 唐极客德2016-06-07看不懂看不懂看不懂
- ljbql2016-07-30是JAVA的也就是一个官方SDK列子
- andywei2016-06-30不错,很有用
kdlqbz
- 粉丝: 29
- 资源: 13
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- AIS2024 valid
- 最入门的爬虫代码 python.docx
- 爬虫零基础入门-爬取天气预报.pdf
- 最通俗易懂的 MongoDB 非结构化文档存储数据库教程.zip
- 以mongodb为数据库的订单物流小项目.zip
- 腾讯云-mongodb数据库, 项目部署.zip
- 腾讯 APIJSON 的 MongoDB 数据库插件.zip
- 理解非关系型数据库和关系型数据库的区别.zip
- 操作简单的Mongodb网页web管理工具,基于Spring Boot2.0支持mongodb集群.zip
- tms-mongodb-web,提供访问mongodb数据的REST API和可灵活扩展的mongodb web 客户端.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功