From 9f3729e04178f174c93779cfaaf8e61f7797547c Mon Sep 17 00:00:00 2001 From: CMM <2198256324@qq.com> Date: Thu, 27 Jul 2023 22:04:56 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=B3=A8=E9=87=8A=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ningdatech/pmapi/irs/sign/IRSAPIRequest.java | 34 +++++++++++----------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/pmapi/src/main/java/com/ningdatech/pmapi/irs/sign/IRSAPIRequest.java b/pmapi/src/main/java/com/ningdatech/pmapi/irs/sign/IRSAPIRequest.java index fc7e84e..9d5792b 100644 --- a/pmapi/src/main/java/com/ningdatech/pmapi/irs/sign/IRSAPIRequest.java +++ b/pmapi/src/main/java/com/ningdatech/pmapi/irs/sign/IRSAPIRequest.java @@ -9,12 +9,15 @@ import com.ningdatech.pmapi.todocenter.model.dto.SignReqDTO; import org.apache.commons.lang3.tuple.Pair; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; +import org.apache.http.client.HttpClient; +import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ByteArrayEntity; import org.apache.http.entity.ContentType; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.impl.client.HttpClients; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; @@ -56,16 +59,9 @@ public class IRSAPIRequest { ReqData.put("posX", posX); ReqData.put("posY", posY); ReqData.put("signType", signType); - // ReqData.put("key", "单位"); ReqData.put("posPage", posPage); ReqData.put("fileName", fileName); resp = post(ReqData, "post"); - - //JSONObject jsonObject = JSON.parseObject(resp); - //String data = jsonObject.getString("data"); - //JSONObject jsondata = JSON.parseObject(data); - //String signFileB64 = jsondata.getString("signFileB64"); - //IRSAPIRequest.base64StringToPdf(signFileB64, "D:\\test21.pdf"); } catch (Exception e) { throw new BizException("调用IRS盖章接口失败,印章编号为:" + sealSn + StrPool.SEMICOLON + e.getMessage()); } @@ -93,16 +89,11 @@ public class IRSAPIRequest { byte[] hash = hasher.doFinal(message.getBytes()); DatatypeConverter.printHexBinary(hash); String sign = DatatypeConverter.printBase64Binary(hash); - /*byte[] stream1 = message.toString().getBytes("UTF-8"); - String sign = sign1(stream1);*/ // 计算电子印章组件signature值 - String myData = data.toString(); - System.out.println(myData); byte[] stream = data.toString().getBytes(StandardCharsets.UTF_8); // 签名数据,根据签名算法,对请求数据进行签名 String signature = sign(stream); - //System.out.println(signature); // 设置HTTP请求头 HttpEntityEnclosingRequestBase req = new HttpPost(IrsSealPlatformProperties.apiUrl); // project-id为用户的projectId @@ -120,9 +111,20 @@ public class IRSAPIRequest { req.setEntity(entity); // 执行请求 - HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); - CloseableHttpClient cli = httpClientBuilder.build(); - HttpResponse res = cli.execute(req); + // 自定义 RequestConfig 设置请求超时时间 + RequestConfig requestConfig = RequestConfig.custom() + // 连接超时时间,单位:毫秒 + .setConnectTimeout(5000) + // 连接请求超时时间,单位:毫秒 + .setConnectionRequestTimeout(5000) + // 数据读取超时时间,单位:毫秒 + .setSocketTimeout(5000) + .build(); + // 通过 HttpClientBuilder 的 custom() 方法创建自定义的 HttpClient + HttpClient customHttpClient = HttpClients.custom() + .setDefaultRequestConfig(requestConfig) + .build(); + HttpResponse res = customHttpClient.execute(req); int statusCode = res.getStatusLine().getStatusCode(); System.out.println(statusCode); if (200 != statusCode) { @@ -134,7 +136,6 @@ public class IRSAPIRequest { byte[] resp = readStream(in); String strRes = new String(resp, StandardCharsets.UTF_8); System.out.println(strRes); - cli.close(); return strRes; } @@ -232,7 +233,6 @@ public class IRSAPIRequest { bout.flush(); byte[] bytes = baos.toByteArray(); return String.valueOf(Base64.getEncoder().encode(bytes)); - //return encoder.encodeBuffer(bytes); } catch (IOException e) { e.printStackTrace();