|
|
@@ -1,6 +1,9 @@
|
|
|
package org.jeecg.common.nbcbutils;
|
|
|
|
|
|
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+
|
|
|
import javax.net.ssl.*;
|
|
|
import java.io.*;
|
|
|
import java.net.HttpURLConnection;
|
|
|
@@ -15,15 +18,16 @@ import java.security.cert.X509Certificate;
|
|
|
* @Date: 2021/9/8 9:36
|
|
|
*/
|
|
|
public class PayNbcbUtils {
|
|
|
+ private static final Logger logger = LoggerFactory.getLogger(PayNbcbUtils.class);
|
|
|
|
|
|
/**
|
|
|
* 向指定URL发送POST请求
|
|
|
*
|
|
|
* @param httpUrl
|
|
|
- * @param Sendstr
|
|
|
+ * @param sendstr
|
|
|
* @return
|
|
|
*/
|
|
|
- public static String doPostForm(String httpUrl, String Sendstr) {
|
|
|
+ public static String doPostForm(String httpUrl, String sendstr) {
|
|
|
|
|
|
HttpURLConnection connection = null;
|
|
|
InputStream is = null;
|
|
|
@@ -35,7 +39,7 @@ public class PayNbcbUtils {
|
|
|
|
|
|
SSLContext sslcontext;
|
|
|
// 获取一个SSLContext实例 TLSv1.2
|
|
|
- sslcontext = SSLContext.getInstance("TLSv1.2");
|
|
|
+ sslcontext = SSLContext.getInstance("SSL", "SunJSSE");
|
|
|
// 初始化SSLContext实例 由于前置机证书是自颁发的,需要绕开证书校验
|
|
|
sslcontext.init(null, new TrustManager[]{new MyX509TrustManager()}, new java.security.SecureRandom());
|
|
|
//由于前置机证书是自颁发的,需要绕开证书校验
|
|
|
@@ -48,29 +52,45 @@ public class PayNbcbUtils {
|
|
|
HttpsURLConnection.setDefaultSSLSocketFactory(sslcontext.getSocketFactory());
|
|
|
|
|
|
// 通过远程url连接对象打开连接
|
|
|
- connection = (HttpsURLConnection) url.openConnection();
|
|
|
+ connection = (HttpURLConnection) url.openConnection();
|
|
|
// 设置连接请求方式
|
|
|
connection.setRequestMethod("POST");
|
|
|
+ // 设置连接超时时长
|
|
|
+ connection.setConnectTimeout(60000);
|
|
|
+ // 设置读取超时
|
|
|
+ connection.setReadTimeout(60000);
|
|
|
// 默认值为:false,当向远程服务器传送数据/写数据时,需要设置为true
|
|
|
connection.setDoOutput(true);
|
|
|
// 默认值为:true,当前向远程服务读取数据时,设置为true,该参数可有可无
|
|
|
connection.setDoInput(true);
|
|
|
// 设置传入参数的格式:请求参数应该是 json形式。
|
|
|
- connection.setRequestProperty("Content-Type", "application/json");
|
|
|
+ connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
|
|
|
+ connection.setRequestProperty("Accept", "application/json");
|
|
|
+ connection.setRequestProperty("Connection", "Keep-Alive");
|
|
|
// 通过连接对象获取一个输出流
|
|
|
os = connection.getOutputStream();
|
|
|
// 通过输出流对象将参数写出去/传输出去,它是通过字节数组写出的(form表单形式的参数实质也是key,value值的拼接,类似于get请求参数的拼接)
|
|
|
- os.write(Sendstr.getBytes("UTF-8"));
|
|
|
+ os.write(sendstr.getBytes("UTF-8"));
|
|
|
// 通过连接对象获取一个输入流,向远程读取
|
|
|
if (connection.getResponseCode() == 200) {
|
|
|
-
|
|
|
is = connection.getInputStream();
|
|
|
// 对输入流对象进行包装:charset根据工作项目组的要求来设置
|
|
|
br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
|
|
|
-
|
|
|
StringBuffer sbf = new StringBuffer();
|
|
|
String temp = null;
|
|
|
- // 循环遍历一行一行读取数据
|
|
|
+ // 获取返回
|
|
|
+ while ((temp = br.readLine()) != null) {
|
|
|
+ sbf.append(temp);
|
|
|
+ sbf.append("\r\n");
|
|
|
+ }
|
|
|
+ result = sbf.toString();
|
|
|
+ } else {
|
|
|
+ is = connection.getInputStream();
|
|
|
+ // 对输入流对象进行包装:charset根据工作项目组的要求来设置
|
|
|
+ br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
|
|
|
+ StringBuffer sbf = new StringBuffer();
|
|
|
+ String temp = null;
|
|
|
+ // 获取错误返回
|
|
|
while ((temp = br.readLine()) != null) {
|
|
|
sbf.append(temp);
|
|
|
sbf.append("\r\n");
|