package hdzx.util;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.KeyManager;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.log4j.Logger;
import org.json.JSONObject;
import com.jfinal.kit.StrKit;
import hdzx.mw.kit.PropertyKit;
public class HttpsUtil {
private static final Logger log = Logger.getLogger(HttpsUtil.class);
private final static String corpid=PropertyKit.getValue(PropertyKit.APIIPPath, "corpid");
private final static String secret=PropertyKit.getValue(PropertyKit.APIIPPath, "secret");
private final static String wxticketurl=PropertyKit.getValue(PropertyKit.APIIPPath, "wxticketurl");
private final static String wxuserurl=PropertyKit.getValue(PropertyKit.APIIPPath, "wxuserurl");
private final static String wxuserinfourl=PropertyKit.getValue(PropertyKit.APIIPPath, "wxuserinfourl");
private final static String ticketurl=PropertyKit.getValue(PropertyKit.APIIPPath, "ticketurl");
private final static String userurl=PropertyKit.getValue(PropertyKit.APIIPPath, "userurl");
private final static String userinfourl=PropertyKit.getValue(PropertyKit.APIIPPath, "userinfourl");
private final static String clientid=PropertyKit.getValue(PropertyKit.APIIPPath, "clientid");
private final static String clientsecret=PropertyKit.getValue(PropertyKit.APIIPPath, "clientsecret");
private static final class DefaultTrustManager implements X509TrustManager {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
}
private static HttpsURLConnection getHttpsURLConnection(String uri, String method) throws IOException {
SSLContext ctx = null;
try {
ctx = SSLContext.getInstance("TLS");
ctx.init(new KeyManager[0], new TrustManager[] { new DefaultTrustManager() }, new SecureRandom());
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
SSLSocketFactory ssf = ctx.getSocketFactory();
URL url = new URL(null, uri, new sun.net.www.protocol.https.Handler());
HttpsURLConnection httpsConn = (HttpsURLConnection) url.openConnection();
httpsConn.setSSLSocketFactory(ssf);
httpsConn.setHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String arg0, SSLSession arg1) {
return true;
}
});
httpsConn.setRequestMethod(method);
httpsConn.setDoInput(true);
httpsConn.setDoOutput(true);
return httpsConn;
}
private static byte[] getBytesFromStream(InputStream is) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] kb = new byte[1024];
int len;
while ((len = is.read(kb)) != -1) {
baos.write(kb, 0, len);
}
byte[] bytes = baos.toByteArray();
baos.close();
is.close();
return bytes;
}
private static void setBytesToStream(OutputStream os, byte[] bytes) throws IOException {
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
byte[] kb = new byte[1024];
int len;
while ((len = bais.read(kb)) != -1) {
os.write(kb, 0, len);
}
os.flush();
os.close();
bais.close();
}
public static byte[] doGet(String uri) throws IOException {
HttpsURLConnection httpsConn = getHttpsURLConnection(uri, "GET");
return getBytesFromStream(httpsConn.getInputStream());
}
public static byte[] doPost(String uri, String data) throws IOException {
HttpsURLConnection httpsConn = getHttpsURLConnection(uri, "POST");
setBytesToStream(httpsConn.getOutputStream(), data.getBytes());
return getBytesFromStream(httpsConn.getInputStream());
}
public static String doGetStr(String uri) throws IOException {
HttpsURLConnection httpsConn = getHttpsURLConnection(uri, "GET");
return new String(getBytesFromStream(httpsConn.getInputStream()));
}
public static String doGetStr(String uri,String encode) throws IOException {
HttpsURLConnection httpsConn = getHttpsURLConnection(uri, "GET");
return new String(getBytesFromStream(httpsConn.getInputStream()),encode);
}
public static String doGetStr(String uri,Map<String, String> headersParams,Map<String, String> mapParams) throws IOException {
String url=setUrlParams(uri, mapParams);
log.info("geturl:"+url);
HttpsURLConnection httpsConn = getHttpsURLConnection(url, "GET");
if (headersParams != null) {
Iterator<String> iterator = headersParams.keySet().iterator();
String key = "";
while (iterator.hasNext()) {
key = iterator.next().toString();
log.info("key:"+key+";"+"value:"+headersParams.get(key));
httpsConn.setRequestProperty(key, headersParams.get(key));
}
}
return new String(getBytesFromStream(httpsConn.getInputStream()));
}
public static String doPostStr(String uri, String data) throws IOException {
HttpsURLConnection httpsConn = getHttpsURLConnection(uri, "POST");
setBytesToStream(httpsConn.getOutputStream(), data.getBytes());
return new String(getBytesFromStream(httpsConn.getInputStream()));
}
public static String doPostStr(String uri, Map<String, Object> params) throws IOException {
HttpsURLConnection httpsConn = getHttpsURLConnection(uri, "POST");
String data="";
if (params != null) {
StringBuilder param = new StringBuilder();
for (Map.Entry<String, Object> entry : params.entrySet()) {
if (param.length() > 0) {
param.append("&");
}
param.append(entry.getKey());
param.append("=");
param.append(entry.getValue());
}
data=param.toString();
System.out.println("param:" + param.toString());
log.info("param:" + param.toString());
}
setBytesToStream(httpsConn.getOutputStream(), data.getBytes());
return new String(getBytesFromStream(httpsConn.getInputStream()));
}
public static String doPostJsonBodyStr(String uri, Map<String, Object> params) throws IOException {
HttpsURLConnection httpsConn = getHttpsURLConnection(uri, "POST");
String data="";
if (params != null) {
StringBuilder param = new StringBuilder();
param.append("{");
for (Map.Entry<String, Object> entry : params.entrySet()) {
param.append("\"").append(entry.getKey()).append("\"");
param.append(":");
tanya123abc
- 粉丝: 2
- 资源: 3
最新资源
- 全氟聚醚行业市场调研报告:全球前10强生产商排名及市场份额
- 自动裁切装PIN设备(含,BOM) sw17可编辑全套技术开发资料100%好用.zip
- C语言编程中圣诞树打印技术实现与教学
- STM32 ADC采样的十种滤波加程序
- 文件上传神器,ftp文件上传到服务器
- (176820022)基于遗传算法(GA)优化高斯过程回归(GA-GPR)的数据回归预测,matlab代码,多变量输入模型 评价指标包括:R2、M
- Python实现控制台打印圣诞树图案
- (176739420)遗传算法(GA)优化极限学习机ELM回归预测,GA-ELM回归预测,多变量输入模型 评价指标包括:R2、MAE、MSE、RM
- (175488410)基于 SSM java源码 仿buy京东商城源码 京东JavaWeb项目源代码+数据库(Java毕业设计,包括源码,教程)
- 自动编带包装机step全套技术开发资料100%好用.zip
- 基于STM32单片机的智能晾衣架项目源码(高分项目)
- 微信小程序开发入门与项目构建指南
- 自动翻转涂胶机(含工程图)sw16可编辑全套技术开发资料100%好用.zip
- (175488396)基于 SSM 的JAVAWEB校园订餐系统项目源码+数据库(Java毕业设计,包括源码,教程).zip
- (177358030)Python 爬虫基金.zip
- LLC板桥震荡参数计算
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈