|
@@ -1,15 +1,18 @@
|
|
|
package com.mrxu.framework.common.util;
|
|
package com.mrxu.framework.common.util;
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
+import org.apache.http.ssl.SSLContexts;
|
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import javax.net.ssl.*;
|
|
import javax.net.ssl.*;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import java.io.*;
|
|
import java.io.*;
|
|
|
|
|
+import java.net.ConnectException;
|
|
|
import java.net.HttpURLConnection;
|
|
import java.net.HttpURLConnection;
|
|
|
import java.net.URL;
|
|
import java.net.URL;
|
|
|
import java.net.URLEncoder;
|
|
import java.net.URLEncoder;
|
|
|
|
|
+import java.security.KeyStore;
|
|
|
import java.security.cert.CertificateException;
|
|
import java.security.cert.CertificateException;
|
|
|
import java.security.cert.X509Certificate;
|
|
import java.security.cert.X509Certificate;
|
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
@@ -395,4 +398,91 @@ public class HttpUtil {
|
|
|
return isAjax(request) || uri.endsWith(".aj") || uri.endsWith(".json");
|
|
return isAjax(request) || uri.endsWith(".aj") || uri.endsWith(".json");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ public static String httpsRequest(String requestUrl, String requestMethod, String outputStr,String certName) {
|
|
|
|
|
+ SSLContext sslContext = null;
|
|
|
|
|
+ HttpsURLConnection conn = null;
|
|
|
|
|
+ InputStream inputStream = null;
|
|
|
|
|
+ InputStreamReader inputStreamReader = null;
|
|
|
|
|
+ BufferedReader bufferedReader = null;
|
|
|
|
|
+ try {
|
|
|
|
|
+ if(certName != null) {
|
|
|
|
|
+ KeyStore keyStore = KeyStore.getInstance("PKCS12");
|
|
|
|
|
+ ClassLoader loader = Thread.currentThread().getContextClassLoader();
|
|
|
|
|
+ InputStream instream = loader.getResourceAsStream("wxcert/" + certName + ".p12");
|
|
|
|
|
+ keyStore.load(instream, certName.toCharArray());
|
|
|
|
|
+ sslContext = SSLContexts.custom().loadKeyMaterial(keyStore, certName.toCharArray()).build();
|
|
|
|
|
+ }
|
|
|
|
|
+ else {
|
|
|
|
|
+ // 创建SSLContext对象,并使用我们指定的信任管理器初始化
|
|
|
|
|
+ TrustManager[] tm = { new MyX509TrustManager() };
|
|
|
|
|
+ sslContext = SSLContext.getInstance("SSL", "SunJSSE");
|
|
|
|
|
+ sslContext.init(null, tm, new java.security.SecureRandom());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 从上述SSLContext对象中得到SSLSocketFactory对象
|
|
|
|
|
+ SSLSocketFactory ssf = sslContext.getSocketFactory();
|
|
|
|
|
+ URL url = new URL(requestUrl);
|
|
|
|
|
+ conn = (HttpsURLConnection) url.openConnection();
|
|
|
|
|
+ conn.setSSLSocketFactory(ssf);
|
|
|
|
|
+ conn.setDoOutput(true);
|
|
|
|
|
+ conn.setDoInput(true);
|
|
|
|
|
+ conn.setUseCaches(false);
|
|
|
|
|
+ // 设置请求方式(GET/POST)
|
|
|
|
|
+ conn.setRequestMethod(requestMethod);
|
|
|
|
|
+ conn.setRequestProperty("content-type", "application/x-www-form-urlencoded");
|
|
|
|
|
+ // 当outputStr不为null时向输出流写数据
|
|
|
|
|
+ if (null != outputStr) {
|
|
|
|
|
+ OutputStream outputStream = conn.getOutputStream();
|
|
|
|
|
+ // 注意编码格式
|
|
|
|
|
+ outputStream.write(outputStr.getBytes("UTF-8"));
|
|
|
|
|
+ outputStream.close();
|
|
|
|
|
+ }
|
|
|
|
|
+ // 从输入流读取返回内容
|
|
|
|
|
+ inputStream = conn.getInputStream();
|
|
|
|
|
+ inputStreamReader = new InputStreamReader(inputStream, "utf-8");
|
|
|
|
|
+ bufferedReader = new BufferedReader(inputStreamReader);
|
|
|
|
|
+ String str = null;
|
|
|
|
|
+ StringBuffer buffer = new StringBuffer();
|
|
|
|
|
+ while ((str = bufferedReader.readLine()) != null) {
|
|
|
|
|
+ buffer.append(str);
|
|
|
|
|
+ }
|
|
|
|
|
+ // 释放资源
|
|
|
|
|
+ return buffer.toString();
|
|
|
|
|
+ }
|
|
|
|
|
+ catch (ConnectException ce) {
|
|
|
|
|
+ logger.error("连接超时:{}", ce);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ logger.error("https请求异常:{}", e);
|
|
|
|
|
+ }
|
|
|
|
|
+ finally {
|
|
|
|
|
+ try {
|
|
|
|
|
+ if(bufferedReader != null) {
|
|
|
|
|
+ bufferedReader.close();
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+ try {
|
|
|
|
|
+ if(inputStreamReader != null) {
|
|
|
|
|
+ inputStreamReader.close();
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+ try {
|
|
|
|
|
+ if(inputStream != null) {
|
|
|
|
|
+ inputStream.close();
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+ inputStream = null;
|
|
|
|
|
+ if(conn != null) {
|
|
|
|
|
+ conn.disconnect();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
}
|
|
}
|