|
@@ -12,10 +12,7 @@ import org.springframework.stereotype.Component;
|
|
|
|
|
|
import javax.net.ssl.*;
|
|
import javax.net.ssl.*;
|
|
import java.io.*;
|
|
import java.io.*;
|
|
-import java.net.ConnectException;
|
|
|
|
-import java.net.SocketTimeoutException;
|
|
|
|
-import java.net.URL;
|
|
|
|
-import java.net.URLConnection;
|
|
|
|
|
|
+import java.net.*;
|
|
import java.security.SecureRandom;
|
|
import java.security.SecureRandom;
|
|
import java.security.cert.X509Certificate;
|
|
import java.security.cert.X509Certificate;
|
|
|
|
|
|
@@ -35,7 +32,7 @@ public class ESignHttpUtil {
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
public JSONObject doPostGetJson(ESignUrl eurl, JSONObject param,String ...querys){
|
|
public JSONObject doPostGetJson(ESignUrl eurl, JSONObject param,String ...querys){
|
|
- String url = eurl.getUrl(querys);
|
|
|
|
|
|
+ String url = eSignConfig.getUrl(eurl,querys);
|
|
String str = doPost(url,param.toJSONString());
|
|
String str = doPost(url,param.toJSONString());
|
|
JSONObject json =JSONObject.parseObject(str);
|
|
JSONObject json =JSONObject.parseObject(str);
|
|
int code = json.getInteger("code");
|
|
int code = json.getInteger("code");
|
|
@@ -53,7 +50,7 @@ public class ESignHttpUtil {
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
public JSONObject doGetGetJson(ESignUrl eurl, JSONObject param,String ...querys){
|
|
public JSONObject doGetGetJson(ESignUrl eurl, JSONObject param,String ...querys){
|
|
- String url = eurl.getUrl(querys);
|
|
|
|
|
|
+ String url = eSignConfig.getUrl(eurl,querys);
|
|
String str = doGet(url);
|
|
String str = doGet(url);
|
|
// String str = doGet(url,param.toJSONString());
|
|
// String str = doGet(url,param.toJSONString());
|
|
JSONObject json =JSONObject.parseObject(str);
|
|
JSONObject json =JSONObject.parseObject(str);
|
|
@@ -72,7 +69,7 @@ public class ESignHttpUtil {
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
public JSONObject doPutGetJson(ESignUrl eurl, JSONObject param,String ...querys){
|
|
public JSONObject doPutGetJson(ESignUrl eurl, JSONObject param,String ...querys){
|
|
- String url = eurl.getUrl(querys);
|
|
|
|
|
|
+ String url = eSignConfig.getUrl(eurl,querys);
|
|
String str = doPut(url,param.toJSONString());
|
|
String str = doPut(url,param.toJSONString());
|
|
JSONObject json =JSONObject.parseObject(str);
|
|
JSONObject json =JSONObject.parseObject(str);
|
|
int code = json.getInteger("code");
|
|
int code = json.getInteger("code");
|
|
@@ -83,7 +80,7 @@ public class ESignHttpUtil {
|
|
}
|
|
}
|
|
|
|
|
|
public String doPost(ESignUrl eurl, JSONObject param,String ...querys){
|
|
public String doPost(ESignUrl eurl, JSONObject param,String ...querys){
|
|
- String url = eurl.getUrl(querys);
|
|
|
|
|
|
+ String url = eSignConfig.getUrl(eurl,querys);
|
|
return doPost(url,param.toJSONString());
|
|
return doPost(url,param.toJSONString());
|
|
}
|
|
}
|
|
|
|
|
|
@@ -95,7 +92,7 @@ public class ESignHttpUtil {
|
|
* @param json 请求参数,
|
|
* @param json 请求参数,
|
|
* @return 所代表远程资源的响应结果
|
|
* @return 所代表远程资源的响应结果
|
|
*/
|
|
*/
|
|
- public String doPost(String url , String json)
|
|
|
|
|
|
+ private String doPost(String url , String json)
|
|
{
|
|
{
|
|
PrintWriter out = null;
|
|
PrintWriter out = null;
|
|
BufferedReader in = null;
|
|
BufferedReader in = null;
|
|
@@ -118,21 +115,30 @@ public class ESignHttpUtil {
|
|
|
|
|
|
conn.setRequestProperty("accept", "*/*");
|
|
conn.setRequestProperty("accept", "*/*");
|
|
conn.setRequestProperty("X-Tsign-Open-App-Id",eSignConfig.getAppId());
|
|
conn.setRequestProperty("X-Tsign-Open-App-Id",eSignConfig.getAppId());
|
|
- conn.setRequestProperty("X-Tsign-Open-Token",eSignConfig.geteSignToken().getAccessToken());
|
|
|
|
|
|
+ conn.setRequestProperty("X-Tsign-Open-Token",eSignConfig.getAccessToken());
|
|
conn.setRequestProperty("connection", "Keep-Alive");
|
|
conn.setRequestProperty("connection", "Keep-Alive");
|
|
conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
|
|
conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
|
|
conn.setUseCaches(false);//设置不要缓存
|
|
conn.setUseCaches(false);//设置不要缓存
|
|
conn.setDoOutput(true);
|
|
conn.setDoOutput(true);
|
|
conn.setDoInput(true);
|
|
conn.setDoInput(true);
|
|
-
|
|
|
|
- out = new PrintWriter(conn.getOutputStream());
|
|
|
|
- out.write(json);
|
|
|
|
- out.flush();
|
|
|
|
- in = new BufferedReader( new InputStreamReader(conn.getInputStream()));
|
|
|
|
String line;
|
|
String line;
|
|
- while ((line = in.readLine()) != null)
|
|
|
|
- {
|
|
|
|
- result.append(line);
|
|
|
|
|
|
+
|
|
|
|
+ try{
|
|
|
|
+ out = new PrintWriter(conn.getOutputStream());
|
|
|
|
+ out.write(json);
|
|
|
|
+ out.flush();
|
|
|
|
+ in = new BufferedReader( new InputStreamReader(conn.getInputStream()));
|
|
|
|
+
|
|
|
|
+ while ((line = in.readLine()) != null)
|
|
|
|
+ {
|
|
|
|
+ result.append(line);
|
|
|
|
+ }
|
|
|
|
+ } catch (IOException e){
|
|
|
|
+ if(conn.getResponseCode()==401){
|
|
|
|
+ log.error("请求E签宝接口报错401,token超时");
|
|
|
|
+ eSignConfig.reSetToken();
|
|
|
|
+ }
|
|
|
|
+ throw e;
|
|
}
|
|
}
|
|
log.info("recv - {}", result);
|
|
log.info("recv - {}", result);
|
|
}
|
|
}
|
|
@@ -158,8 +164,6 @@ public class ESignHttpUtil {
|
|
}
|
|
}
|
|
finally
|
|
finally
|
|
{
|
|
{
|
|
- //清除token缓存
|
|
|
|
- eSignConfig.geteSignToken().reSetToken();
|
|
|
|
try
|
|
try
|
|
{
|
|
{
|
|
if (out != null)
|
|
if (out != null)
|
|
@@ -186,7 +190,7 @@ public class ESignHttpUtil {
|
|
* @param json 请求参数,
|
|
* @param json 请求参数,
|
|
* @return 所代表远程资源的响应结果
|
|
* @return 所代表远程资源的响应结果
|
|
*/
|
|
*/
|
|
- public String doPut(String url , String json)
|
|
|
|
|
|
+ private String doPut(String url , String json)
|
|
{
|
|
{
|
|
PrintWriter out = null;
|
|
PrintWriter out = null;
|
|
BufferedReader in = null;
|
|
BufferedReader in = null;
|
|
@@ -209,20 +213,29 @@ public class ESignHttpUtil {
|
|
conn.setRequestMethod("PUT");
|
|
conn.setRequestMethod("PUT");
|
|
conn.setRequestProperty("accept", "*/*");
|
|
conn.setRequestProperty("accept", "*/*");
|
|
conn.setRequestProperty("X-Tsign-Open-App-Id",eSignConfig.getAppId());
|
|
conn.setRequestProperty("X-Tsign-Open-App-Id",eSignConfig.getAppId());
|
|
- conn.setRequestProperty("X-Tsign-Open-Token",eSignConfig.geteSignToken().getAccessToken());
|
|
|
|
|
|
+ conn.setRequestProperty("X-Tsign-Open-Token",eSignConfig.getAccessToken());
|
|
conn.setRequestProperty("connection", "Keep-Alive");
|
|
conn.setRequestProperty("connection", "Keep-Alive");
|
|
conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
|
|
conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
|
|
conn.setUseCaches(false);//设置不要缓存
|
|
conn.setUseCaches(false);//设置不要缓存
|
|
conn.setDoOutput(true);
|
|
conn.setDoOutput(true);
|
|
conn.setDoInput(true);
|
|
conn.setDoInput(true);
|
|
- out = new PrintWriter(conn.getOutputStream());
|
|
|
|
- out.write(json);
|
|
|
|
- out.flush();
|
|
|
|
- in = new BufferedReader( new InputStreamReader(conn.getInputStream()));
|
|
|
|
String line;
|
|
String line;
|
|
- while ((line = in.readLine()) != null)
|
|
|
|
- {
|
|
|
|
- result.append(line);
|
|
|
|
|
|
+ try{
|
|
|
|
+ out = new PrintWriter(conn.getOutputStream());
|
|
|
|
+ out.write(json);
|
|
|
|
+ out.flush();
|
|
|
|
+ in = new BufferedReader( new InputStreamReader(conn.getInputStream()));
|
|
|
|
+
|
|
|
|
+ while ((line = in.readLine()) != null)
|
|
|
|
+ {
|
|
|
|
+ result.append(line);
|
|
|
|
+ }
|
|
|
|
+ } catch (IOException e){
|
|
|
|
+ if(conn.getResponseCode()==401){
|
|
|
|
+ log.error("请求E签宝接口报错401,token超时");
|
|
|
|
+ eSignConfig.reSetToken();
|
|
|
|
+ }
|
|
|
|
+ throw e;
|
|
}
|
|
}
|
|
log.info("recv - {}", result);
|
|
log.info("recv - {}", result);
|
|
}
|
|
}
|
|
@@ -239,6 +252,7 @@ public class ESignHttpUtil {
|
|
catch (IOException e)
|
|
catch (IOException e)
|
|
{
|
|
{
|
|
log.error("调用ESignHttpUtil.doPost IOException, url=" + url + ",param=" + json, e);
|
|
log.error("调用ESignHttpUtil.doPost IOException, url=" + url + ",param=" + json, e);
|
|
|
|
+
|
|
throw new BusinessException("调用ESignHttpUtil.sendJsonPost IOException, url=" + url + ",param=" + json );
|
|
throw new BusinessException("调用ESignHttpUtil.sendJsonPost IOException, url=" + url + ",param=" + json );
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
catch (Exception e)
|
|
@@ -248,8 +262,7 @@ public class ESignHttpUtil {
|
|
}
|
|
}
|
|
finally
|
|
finally
|
|
{
|
|
{
|
|
- //清除token缓存
|
|
|
|
- eSignConfig.geteSignToken().reSetToken();
|
|
|
|
|
|
+
|
|
try
|
|
try
|
|
{
|
|
{
|
|
if (out != null)
|
|
if (out != null)
|
|
@@ -269,7 +282,7 @@ public class ESignHttpUtil {
|
|
return result.toString();
|
|
return result.toString();
|
|
}
|
|
}
|
|
|
|
|
|
- public String doGet(String url, String json)
|
|
|
|
|
|
+ private String doGet(String url, String json)
|
|
{
|
|
{
|
|
PrintWriter out = null;
|
|
PrintWriter out = null;
|
|
StringBuilder result = new StringBuilder();
|
|
StringBuilder result = new StringBuilder();
|
|
@@ -280,21 +293,35 @@ public class ESignHttpUtil {
|
|
log.info("sendGet - {}", urlNameString);
|
|
log.info("sendGet - {}", urlNameString);
|
|
URL realUrl = new URL(urlNameString);
|
|
URL realUrl = new URL(urlNameString);
|
|
|
|
|
|
- URLConnection connection = realUrl.openConnection();
|
|
|
|
|
|
+ HttpURLConnection connection = (HttpURLConnection)realUrl.openConnection();
|
|
connection.setRequestProperty("accept", "*/*");
|
|
connection.setRequestProperty("accept", "*/*");
|
|
connection.setRequestProperty("X-Tsign-Open-App-Id",eSignConfig.getAppId());
|
|
connection.setRequestProperty("X-Tsign-Open-App-Id",eSignConfig.getAppId());
|
|
- connection.setRequestProperty("X-Tsign-Open-Token",eSignConfig.geteSignToken().getAccessToken());
|
|
|
|
|
|
+ connection.setRequestProperty("X-Tsign-Open-Token",eSignConfig.getAccessToken());
|
|
connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
|
|
connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
|
|
connection.setRequestProperty("connection", "Keep-Alive");
|
|
connection.setRequestProperty("connection", "Keep-Alive");
|
|
connection.connect();
|
|
connection.connect();
|
|
- in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
|
|
|
- out = new PrintWriter(connection.getOutputStream());
|
|
|
|
- out.write(json);
|
|
|
|
- out.flush();
|
|
|
|
|
|
+
|
|
String line;
|
|
String line;
|
|
- while ((line = in.readLine()) != null)
|
|
|
|
- {
|
|
|
|
- result.append(line);
|
|
|
|
|
|
+
|
|
|
|
+ try{
|
|
|
|
+ if(json!=null){
|
|
|
|
+ out = new PrintWriter(connection.getOutputStream());
|
|
|
|
+ out.write(json);
|
|
|
|
+ out.flush();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ in = new BufferedReader( new InputStreamReader(connection.getInputStream()));
|
|
|
|
+
|
|
|
|
+ while ((line = in.readLine()) != null)
|
|
|
|
+ {
|
|
|
|
+ result.append(line);
|
|
|
|
+ }
|
|
|
|
+ } catch (IOException e){
|
|
|
|
+ if(connection.getResponseCode()==401){
|
|
|
|
+ log.error("请求E签宝接口报错401,token超时");
|
|
|
|
+ eSignConfig.reSetToken();
|
|
|
|
+ }
|
|
|
|
+ throw e;
|
|
}
|
|
}
|
|
log.info("recv - {}", result);
|
|
log.info("recv - {}", result);
|
|
}
|
|
}
|
|
@@ -320,8 +347,7 @@ public class ESignHttpUtil {
|
|
}
|
|
}
|
|
finally
|
|
finally
|
|
{
|
|
{
|
|
- //清除token缓存
|
|
|
|
- eSignConfig.geteSignToken().reSetToken();
|
|
|
|
|
|
+
|
|
try
|
|
try
|
|
{
|
|
{
|
|
if (out != null)
|
|
if (out != null)
|
|
@@ -341,68 +367,9 @@ public class ESignHttpUtil {
|
|
return result.toString();
|
|
return result.toString();
|
|
}
|
|
}
|
|
|
|
|
|
- public String doGet(String url)
|
|
|
|
|
|
+ private 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.setRequestProperty("X-Tsign-Open-App-Id",eSignConfig.getAppId());
|
|
|
|
- connection.setRequestProperty("X-Tsign-Open-Token",eSignConfig.geteSignToken().getAccessToken());
|
|
|
|
- connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
|
|
|
|
- connection.setRequestProperty("connection", "Keep-Alive");
|
|
|
|
- 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
|
|
|
|
- {
|
|
|
|
- //清除token缓存
|
|
|
|
- eSignConfig.geteSignToken().reSetToken();
|
|
|
|
- try
|
|
|
|
- {
|
|
|
|
- if (in != null)
|
|
|
|
- {
|
|
|
|
- in.close();
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- catch (Exception ex)
|
|
|
|
- {
|
|
|
|
- log.error("调用in.close Exception, url=" + url , ex);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- return result.toString();
|
|
|
|
|
|
+ return doGet(url,null);
|
|
}
|
|
}
|
|
|
|
|
|
private static class TrustAnyTrustManager implements X509TrustManager
|
|
private static class TrustAnyTrustManager implements X509TrustManager
|