|
|
@@ -7,8 +7,10 @@ import java.net.URL;
|
|
|
import java.security.KeyManagementException;
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
import java.security.NoSuchProviderException;
|
|
|
+import java.security.SecureRandom;
|
|
|
|
|
|
import com.jeeplus.common.utils.MyX509TrustManager;
|
|
|
+import com.jeeplus.common.utils.StringUtils;
|
|
|
import com.jeeplus.modules.utils.Aestool;
|
|
|
import com.jeeplus.modules.utils.Rsatool;
|
|
|
import net.sf.json.JSONObject;
|
|
|
@@ -27,10 +29,7 @@ import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
import org.apache.http.impl.client.HttpClients;
|
|
|
import org.apache.http.util.EntityUtils;
|
|
|
|
|
|
-import javax.net.ssl.HttpsURLConnection;
|
|
|
-import javax.net.ssl.SSLContext;
|
|
|
-import javax.net.ssl.SSLSocketFactory;
|
|
|
-import javax.net.ssl.TrustManager;
|
|
|
+import javax.net.ssl.*;
|
|
|
|
|
|
/**
|
|
|
* @author: 大猫
|
|
|
@@ -159,57 +158,92 @@ public class HttpPostTool {
|
|
|
* @return JSONObject(通过JSONObject.get(key)的方式获取json对象的属性值)
|
|
|
*/
|
|
|
public static String doPost(String requestUrl, String outputStr) {
|
|
|
+ // 空值防护
|
|
|
+ if (StringUtils.isBlank(requestUrl)) {
|
|
|
+ System.err.println("请求URL不能为空");
|
|
|
+ return "{}";
|
|
|
+ }
|
|
|
+
|
|
|
JSONObject jsonObject = null;
|
|
|
- StringBuffer buffer = new StringBuffer();
|
|
|
+ HttpURLConnection httpUrlConn = null;
|
|
|
+
|
|
|
try {
|
|
|
- // 创建SSLContext对象,并使用我们指定的信任管理器初始化
|
|
|
- TrustManager[] tm = { new MyX509TrustManager() };
|
|
|
- SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
|
|
|
- sslContext.init(null, tm, new java.security.SecureRandom());
|
|
|
- // 从上述SSLContext对象中得到SSLSocketFactory对象
|
|
|
- SSLSocketFactory ssf = sslContext.getSocketFactory();
|
|
|
+ URL url = new URL(requestUrl); // 移除sun内部类,自动适配HTTP/HTTPS
|
|
|
+ httpUrlConn = (HttpURLConnection) url.openConnection();
|
|
|
|
|
|
- //URL url = new URL(requestUrl);
|
|
|
- URL url= new URL(null, requestUrl, new sun.net.www.protocol.https.Handler());
|
|
|
- HttpsURLConnection httpUrlConn = (HttpsURLConnection) url.openConnection();
|
|
|
- httpUrlConn.setSSLSocketFactory(ssf);
|
|
|
+ // 若为HTTPS连接,配置SSL
|
|
|
+ if (httpUrlConn instanceof HttpsURLConnection) {
|
|
|
+ HttpsURLConnection httpsConn = (HttpsURLConnection) httpUrlConn;
|
|
|
+ // 初始化SSL上下文
|
|
|
+ SSLContext sslContext = SSLContext.getInstance("TLS"); // 替换SSL为TLS,更安全
|
|
|
+ sslContext.init(null, new TrustManager[]{new MyX509TrustManager()}, new SecureRandom());
|
|
|
+ httpsConn.setSSLSocketFactory(sslContext.getSocketFactory());
|
|
|
+ // 忽略主机名验证(可选,生产需关闭)
|
|
|
+ httpsConn.setHostnameVerifier((hostname, session) -> true);
|
|
|
+ }
|
|
|
|
|
|
+ // 通用连接配置
|
|
|
httpUrlConn.setDoOutput(true);
|
|
|
httpUrlConn.setDoInput(true);
|
|
|
httpUrlConn.setUseCaches(false);
|
|
|
httpUrlConn.setRequestMethod("POST");
|
|
|
+ httpUrlConn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
|
|
|
|
|
|
- // 当有数据需要提交时
|
|
|
- if (null != outputStr) {
|
|
|
- OutputStream outputStream = httpUrlConn.getOutputStream();
|
|
|
- // 注意编码格式,防止中文乱码
|
|
|
- outputStream.write(outputStr.getBytes("UTF-8"));
|
|
|
- outputStream.close();
|
|
|
+ // 提交请求参数
|
|
|
+ if (StringUtils.isNotBlank(outputStr)) {
|
|
|
+ try (OutputStream outputStream = httpUrlConn.getOutputStream()) {
|
|
|
+ outputStream.write(outputStr.getBytes("UTF-8"));
|
|
|
+ outputStream.flush();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- // 将返回的输入流转换成字符串
|
|
|
- InputStream inputStream = httpUrlConn.getInputStream();
|
|
|
- InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
|
|
|
- BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
|
|
|
+ // 读取响应(try-with-resources自动关闭资源)
|
|
|
+ int responseCode = httpUrlConn.getResponseCode();
|
|
|
+ if (responseCode == HttpURLConnection.HTTP_OK) {
|
|
|
+ try (InputStream inputStream = httpUrlConn.getInputStream();
|
|
|
+ InputStreamReader isr = new InputStreamReader(inputStream, "UTF-8");
|
|
|
+ BufferedReader br = new BufferedReader(isr)) {
|
|
|
|
|
|
- String str = null;
|
|
|
- while ((str = bufferedReader.readLine()) != null) {
|
|
|
- buffer.append(str);
|
|
|
+ StringBuffer buffer = new StringBuffer();
|
|
|
+ String str;
|
|
|
+ while ((str = br.readLine()) != null) {
|
|
|
+ buffer.append(str);
|
|
|
+ }
|
|
|
+ // 容错:仅当返回非空时解析JSON
|
|
|
+ if (StringUtils.isNotBlank(buffer.toString())) {
|
|
|
+ jsonObject = JSONObject.fromObject(buffer.toString());
|
|
|
+ } else {
|
|
|
+ jsonObject = new JSONObject(); // 空JSON
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ System.err.println("接口返回错误码:" + responseCode);
|
|
|
+ jsonObject = new JSONObject();
|
|
|
+ jsonObject.put("errorCode", responseCode);
|
|
|
}
|
|
|
- bufferedReader.close();
|
|
|
- inputStreamReader.close();
|
|
|
- // 释放资源
|
|
|
- inputStream.close();
|
|
|
- inputStream = null;
|
|
|
- httpUrlConn.disconnect();
|
|
|
- jsonObject = JSONObject.fromObject(buffer.toString());
|
|
|
+
|
|
|
+ } catch (SSLException e) {
|
|
|
+ System.err.println("HTTPS SSL认证失败:" + e.getMessage());
|
|
|
+ jsonObject = new JSONObject();
|
|
|
+ jsonObject.put("error", "SSL认证失败");
|
|
|
+ } catch (IOException e) {
|
|
|
+ System.err.println("IO异常(连接/读写失败):" + e.getMessage());
|
|
|
+ jsonObject = new JSONObject();
|
|
|
+ jsonObject.put("error", "连接失败");
|
|
|
} catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- System.out.println("上报接口错误:" + e);
|
|
|
+ System.err.println("未知异常:" + e.getMessage());
|
|
|
+ jsonObject = new JSONObject();
|
|
|
+ jsonObject.put("error", "解析失败");
|
|
|
} finally {
|
|
|
- System.out.println(jsonObject.toString());
|
|
|
+ // 关闭连接
|
|
|
+ if (httpUrlConn != null) {
|
|
|
+ httpUrlConn.disconnect();
|
|
|
+ }
|
|
|
+ // 空值防护:避免NPE
|
|
|
+ System.out.println(jsonObject == null ? "{}" : jsonObject.toString());
|
|
|
}
|
|
|
- return jsonObject.toString();
|
|
|
+
|
|
|
+ return jsonObject == null ? "{}" : jsonObject.toString();
|
|
|
}
|
|
|
|
|
|
|