|
@@ -1,11 +1,17 @@
|
|
|
package com.jeeplus.modules.tools.utils;
|
|
|
|
|
|
import java.io.*;
|
|
|
+import java.net.ConnectException;
|
|
|
import java.net.HttpURLConnection;
|
|
|
import java.net.URL;
|
|
|
+import java.security.KeyManagementException;
|
|
|
+import java.security.NoSuchAlgorithmException;
|
|
|
+import java.security.NoSuchProviderException;
|
|
|
|
|
|
+import com.jeeplus.common.utils.MyX509TrustManager;
|
|
|
import com.jeeplus.modules.utils.Aestool;
|
|
|
import com.jeeplus.modules.utils.Rsatool;
|
|
|
+import net.sf.json.JSONObject;
|
|
|
import org.apache.commons.codec.binary.Base64;
|
|
|
import org.apache.commons.httpclient.HttpClient;
|
|
|
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
|
|
@@ -21,6 +27,11 @@ 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;
|
|
|
+
|
|
|
/**
|
|
|
* @author: 大猫
|
|
|
* @create: 2021-06-07 17:49
|
|
@@ -94,10 +105,10 @@ public class HttpPostTool {
|
|
|
OutputStreamWriter out = null;
|
|
|
BufferedReader in = null;
|
|
|
StringBuilder result = new StringBuilder();
|
|
|
- HttpURLConnection conn = null;
|
|
|
+ HttpsURLConnection conn = null;
|
|
|
try{
|
|
|
URL url = new URL(requestUrl);
|
|
|
- conn = (HttpURLConnection) url.openConnection();
|
|
|
+ conn = (HttpsURLConnection) url.openConnection();
|
|
|
conn.setRequestMethod("POST");
|
|
|
//发送POST请求必须设置为true
|
|
|
conn.setDoOutput(true);
|
|
@@ -140,7 +151,68 @@ public class HttpPostTool {
|
|
|
return result.toString();
|
|
|
}*/
|
|
|
|
|
|
- public static String doPost(String url, String jsonString) {
|
|
|
+ /**
|
|
|
+ * 1.发起https请求并获取结果
|
|
|
+ *
|
|
|
+ * @param requestUrl 请求地址
|
|
|
+ * @param outputStr 提交的数据
|
|
|
+ * @return JSONObject(通过JSONObject.get(key)的方式获取json对象的属性值)
|
|
|
+ */
|
|
|
+ public static String doPost(String requestUrl, String outputStr) {
|
|
|
+ JSONObject jsonObject = null;
|
|
|
+ StringBuffer buffer = new StringBuffer();
|
|
|
+ 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);
|
|
|
+ HttpsURLConnection httpUrlConn = (HttpsURLConnection) url.openConnection();
|
|
|
+ httpUrlConn.setSSLSocketFactory(ssf);
|
|
|
+
|
|
|
+ httpUrlConn.setDoOutput(true);
|
|
|
+ httpUrlConn.setDoInput(true);
|
|
|
+ httpUrlConn.setUseCaches(false);
|
|
|
+ httpUrlConn.setRequestMethod("POST");
|
|
|
+
|
|
|
+ // 当有数据需要提交时
|
|
|
+ if (null != outputStr) {
|
|
|
+ OutputStream outputStream = httpUrlConn.getOutputStream();
|
|
|
+ // 注意编码格式,防止中文乱码
|
|
|
+ outputStream.write(outputStr.getBytes("UTF-8"));
|
|
|
+ outputStream.close();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 将返回的输入流转换成字符串
|
|
|
+ InputStream inputStream = httpUrlConn.getInputStream();
|
|
|
+ InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
|
|
|
+ BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
|
|
|
+
|
|
|
+ String str = null;
|
|
|
+ while ((str = bufferedReader.readLine()) != null) {
|
|
|
+ buffer.append(str);
|
|
|
+ }
|
|
|
+ bufferedReader.close();
|
|
|
+ inputStreamReader.close();
|
|
|
+ // 释放资源
|
|
|
+ inputStream.close();
|
|
|
+ inputStream = null;
|
|
|
+ httpUrlConn.disconnect();
|
|
|
+ jsonObject = JSONObject.fromObject(buffer.toString());
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ System.out.println(jsonObject.toString());
|
|
|
+ }
|
|
|
+ return jsonObject.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /*public static String doPost(String url, String jsonString) {
|
|
|
CloseableHttpClient httpClient = null;
|
|
|
CloseableHttpResponse httpResponse = null;
|
|
|
String result = "";
|
|
@@ -194,7 +266,7 @@ public class HttpPostTool {
|
|
|
}
|
|
|
System.out.println(result);
|
|
|
return result;
|
|
|
- }
|
|
|
+ }*/
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|