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(":");