|
@@ -0,0 +1,214 @@
|
|
|
|
+package com.dgtly.wxportal.utils.ESign;
|
|
|
|
+
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
+import com.dgtly.common.exception.BusinessException;
|
|
|
|
+import com.dgtly.common.utils.http.HttpUtils;
|
|
|
|
+import com.dgtly.wxportal.config.ESignConfig;
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
+
|
|
|
|
+import javax.net.ssl.*;
|
|
|
|
+import java.io.BufferedReader;
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.io.InputStreamReader;
|
|
|
|
+import java.io.PrintWriter;
|
|
|
|
+import java.net.ConnectException;
|
|
|
|
+import java.net.SocketTimeoutException;
|
|
|
|
+import java.net.URL;
|
|
|
|
+import java.net.URLConnection;
|
|
|
|
+import java.security.SecureRandom;
|
|
|
|
+import java.security.cert.X509Certificate;
|
|
|
|
+
|
|
|
|
+@Component
|
|
|
|
+public class ESignHttpUtil {
|
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(ESignHttpUtil.class);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private ESignConfig eSignConfig;
|
|
|
|
+
|
|
|
|
+ public String doPost(ESignUrl eurl, JSONObject param,String ...querys){
|
|
|
|
+ String url = eurl.getUrl(querys);
|
|
|
|
+ return doPost(url,param.toJSONString());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public String doPost(ESignUrl eurl, JSONObject param){
|
|
|
|
+ String url = eurl.getUrl();
|
|
|
|
+ return doPost(url,param.toJSONString());
|
|
|
|
+ }
|
|
|
|
+ /**
|
|
|
|
+ * 向指定 URL 发送POST方法的请求
|
|
|
|
+ *
|
|
|
|
+ * @param url 发送请求的 URL
|
|
|
|
+ * @param json 请求参数,
|
|
|
|
+ * @return 所代表远程资源的响应结果
|
|
|
|
+ */
|
|
|
|
+ public String doPost(String url , String json)
|
|
|
|
+ {
|
|
|
|
+ PrintWriter out = null;
|
|
|
|
+ BufferedReader in = null;
|
|
|
|
+ StringBuilder result = new StringBuilder();
|
|
|
|
+ try
|
|
|
|
+ {
|
|
|
|
+
|
|
|
|
+ log.info("sendPost - {}", url);
|
|
|
|
+ // 打开和URL之间的连接
|
|
|
|
+ SSLContext sc = SSLContext.getInstance("SSL");
|
|
|
|
+ sc.init(null, new TrustManager[] { new TrustAnyTrustManager() }, new SecureRandom());
|
|
|
|
+ // 从上述SSLContext对象中得到SSLSocketFactory对象
|
|
|
|
+ SSLSocketFactory ssf = sc.getSocketFactory();
|
|
|
|
+
|
|
|
|
+ java.net.URL realUrl = new URL(url);
|
|
|
|
+
|
|
|
|
+ HttpsURLConnection conn = (HttpsURLConnection) realUrl.openConnection();
|
|
|
|
+
|
|
|
|
+ conn.setSSLSocketFactory(ssf);
|
|
|
|
+
|
|
|
|
+ conn.setRequestProperty("accept", "*/*");
|
|
|
|
+ conn.setRequestProperty("X-Tsign-Open-App-Id",eSignConfig.getAppId());
|
|
|
|
+ conn.setRequestProperty("X-Tsign-Open-Token",eSignConfig.geteSignToken().getAccessToken());
|
|
|
|
+ conn.setRequestProperty("connection", "Keep-Alive");
|
|
|
|
+ conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
|
|
|
|
+ conn.setUseCaches(false);//设置不要缓存
|
|
|
|
+ conn.setDoOutput(true);
|
|
|
|
+ conn.setDoInput(true);
|
|
|
|
+ out = new PrintWriter(conn.getOutputStream());
|
|
|
|
+ out.write(json);
|
|
|
|
+ out.flush();
|
|
|
|
+ in = new BufferedReader( new InputStreamReader(conn.getInputStream()));
|
|
|
|
+ String line;
|
|
|
|
+ while ((line = in.readLine()) != null)
|
|
|
|
+ {
|
|
|
|
+ result.append(line);
|
|
|
|
+ }
|
|
|
|
+ log.info("recv - {}", result);
|
|
|
|
+ }
|
|
|
|
+ catch (ConnectException e)
|
|
|
|
+ {
|
|
|
|
+ log.error("调用ESignHttpUtil.sendJsonPost ConnectException, url=" + url + ",param_body=" + json, e);
|
|
|
|
+ throw new BusinessException("调用ESignHttpUtil.sendJsonPost ConnectException, url=" + url + ",param=" + json );
|
|
|
|
+ }
|
|
|
|
+ catch (SocketTimeoutException e)
|
|
|
|
+ {
|
|
|
|
+ log.error("调用ESignHttpUtil.sendJsonPost SocketTimeoutException, url=" + url + ",param=" + json, e);
|
|
|
|
+ throw new BusinessException("调用ESignHttpUtil.sendJsonPost SocketTimeoutException, url=" + url + ",param=" + json );
|
|
|
|
+ }
|
|
|
|
+ catch (IOException e)
|
|
|
|
+ {
|
|
|
|
+ log.error("调用ESignHttpUtil.sendJsonPost IOException, url=" + url + ",param=" + json, e);
|
|
|
|
+ throw new BusinessException("调用ESignHttpUtil.sendJsonPost IOException, url=" + url + ",param=" + json );
|
|
|
|
+ }
|
|
|
|
+ catch (Exception e)
|
|
|
|
+ {
|
|
|
|
+ log.error("调用HttpsUtil.sendJsonPost Exception, url=" + url + ",param=" + json, e);
|
|
|
|
+ throw new BusinessException("调用ESignHttpUtil.sendJsonPost Exception, url=" + url + ",param=" + json );
|
|
|
|
+ }
|
|
|
|
+ finally
|
|
|
|
+ {
|
|
|
|
+ try
|
|
|
|
+ {
|
|
|
|
+ if (out != null)
|
|
|
|
+ {
|
|
|
|
+ out.close();
|
|
|
|
+ }
|
|
|
|
+ if (in != null)
|
|
|
|
+ {
|
|
|
|
+ in.close();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ catch (IOException ex)
|
|
|
|
+ {
|
|
|
|
+ log.error("调用in.close Exception, url=" + url + ",param=" + json, ex);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return result.toString();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static String doGet(String url)
|
|
|
|
+ {
|
|
|
|
+ StringBuilder result = new StringBuilder();
|
|
|
|
+ BufferedReader in = null;
|
|
|
|
+ try
|
|
|
|
+ {
|
|
|
|
+ String urlNameString = url;
|
|
|
|
+ log.info("sendGet - {}", urlNameString);
|
|
|
|
+ URL realUrl = new URL(urlNameString);
|
|
|
|
+
|
|
|
|
+ URLConnection connection = realUrl.openConnection();
|
|
|
|
+ connection.setRequestProperty("accept", "*/*");
|
|
|
|
+ connection.connect();
|
|
|
|
+ in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
|
|
|
+ String line;
|
|
|
|
+ while ((line = in.readLine()) != null)
|
|
|
|
+ {
|
|
|
|
+ result.append(line);
|
|
|
|
+ }
|
|
|
|
+ log.info("recv - {}", result);
|
|
|
|
+ }
|
|
|
|
+ catch(ConnectException e)
|
|
|
|
+ {
|
|
|
|
+ log.error("调用ESignHttpUtil.sendGet ConnectException, url=" + url , e);
|
|
|
|
+ throw new BusinessException("调用ESignHttpUtil.sendGet ConnectException, url=" + url );
|
|
|
|
+ }
|
|
|
|
+ catch (SocketTimeoutException e)
|
|
|
|
+ {
|
|
|
|
+ log.error("调用ESignHttpUtil.sendGet SocketTimeoutException, url=" + url , e);
|
|
|
|
+ throw new BusinessException("调用ESignHttpUtil.sendGet SocketTimeoutException, url=" + url );
|
|
|
|
+ }
|
|
|
|
+ catch (IOException e)
|
|
|
|
+ {
|
|
|
|
+ log.error("调用ESignHttpUtil.sendGet IOException, url=" + url, e);
|
|
|
|
+ throw new BusinessException("调用ESignHttpUtil.sendGet IOException, url=" + url );
|
|
|
|
+ }
|
|
|
|
+ catch (Exception e)
|
|
|
|
+ {
|
|
|
|
+ log.error("调用HttpsUtil.sendGet Exception, url=" + url , e);
|
|
|
|
+ throw new BusinessException("调用ESignHttpUtil.sendGet Exception, url=" + url );
|
|
|
|
+ }
|
|
|
|
+ finally
|
|
|
|
+ {
|
|
|
|
+ try
|
|
|
|
+ {
|
|
|
|
+ if (in != null)
|
|
|
|
+ {
|
|
|
|
+ in.close();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ catch (Exception ex)
|
|
|
|
+ {
|
|
|
|
+ log.error("调用in.close Exception, url=" + url , ex);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return result.toString();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static class TrustAnyTrustManager implements X509TrustManager
|
|
|
|
+ {
|
|
|
|
+ @Override
|
|
|
|
+ public void checkClientTrusted(X509Certificate[] chain, String authType)
|
|
|
|
+ {
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void checkServerTrusted(X509Certificate[] chain, String authType)
|
|
|
|
+ {
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public X509Certificate[] getAcceptedIssuers()
|
|
|
|
+ {
|
|
|
|
+ return new X509Certificate[] {};
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static class TrustAnyHostnameVerifier implements HostnameVerifier
|
|
|
|
+ {
|
|
|
|
+ @Override
|
|
|
|
+ public boolean verify(String hostname, SSLSession session)
|
|
|
|
+ {
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|