|
@@ -1,25 +1,17 @@
|
|
|
package com.usky.dxtop.common.utils.http;
|
|
|
|
|
|
-import java.io.BufferedReader;
|
|
|
-import java.io.IOException;
|
|
|
-import java.io.InputStream;
|
|
|
-import java.io.InputStreamReader;
|
|
|
-import java.io.PrintWriter;
|
|
|
+import com.usky.dxtop.common.constant.Constants;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+
|
|
|
+import javax.net.ssl.*;
|
|
|
+import java.io.*;
|
|
|
import java.net.ConnectException;
|
|
|
import java.net.SocketTimeoutException;
|
|
|
import java.net.URL;
|
|
|
import java.net.URLConnection;
|
|
|
import java.security.cert.X509Certificate;
|
|
|
-import javax.net.ssl.HostnameVerifier;
|
|
|
-import javax.net.ssl.HttpsURLConnection;
|
|
|
-import javax.net.ssl.SSLContext;
|
|
|
-import javax.net.ssl.SSLSession;
|
|
|
-import javax.net.ssl.TrustManager;
|
|
|
-import javax.net.ssl.X509TrustManager;
|
|
|
-
|
|
|
-import com.usky.dxtop.common.constant.Constants;
|
|
|
-import org.slf4j.Logger;
|
|
|
-import org.slf4j.LoggerFactory;
|
|
|
+import java.util.Map;
|
|
|
|
|
|
|
|
|
/**
|
|
@@ -38,9 +30,9 @@ public class HttpUtils
|
|
|
* @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
|
|
|
* @return 所代表远程资源的响应结果
|
|
|
*/
|
|
|
- public static String sendGet(String url, String param)
|
|
|
+ public static String sendGet(String url, String param, Map<String,String> map)
|
|
|
{
|
|
|
- return sendGet(url, param, Constants.UTF8);
|
|
|
+ return sendGet(url, param, Constants.UTF8,map);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -51,7 +43,7 @@ public class HttpUtils
|
|
|
* @param contentType 编码类型
|
|
|
* @return 所代表远程资源的响应结果
|
|
|
*/
|
|
|
- public static String sendGet(String url, String param, String contentType)
|
|
|
+ public static String sendGet(String url, String param, String contentType, Map<String,String> map)
|
|
|
{
|
|
|
StringBuilder result = new StringBuilder();
|
|
|
BufferedReader in = null;
|
|
@@ -64,6 +56,11 @@ public class HttpUtils
|
|
|
connection.setRequestProperty("accept", "*/*");
|
|
|
connection.setRequestProperty("connection", "Keep-Alive");
|
|
|
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
|
|
+ if (null != map && !map.isEmpty()){
|
|
|
+ for (String key:map.keySet()) {
|
|
|
+ connection.setRequestProperty(key,map.get(key));
|
|
|
+ }
|
|
|
+ }
|
|
|
connection.connect();
|
|
|
in = new BufferedReader(new InputStreamReader(connection.getInputStream(), contentType));
|
|
|
String line;
|
|
@@ -113,7 +110,7 @@ public class HttpUtils
|
|
|
* @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
|
|
|
* @return 所代表远程资源的响应结果
|
|
|
*/
|
|
|
- public static String sendPost(String url, String param)
|
|
|
+ public static String sendPost(String url, String param, Map<String,String> map)
|
|
|
{
|
|
|
PrintWriter out = null;
|
|
|
BufferedReader in = null;
|
|
@@ -131,6 +128,11 @@ public class HttpUtils
|
|
|
conn.setRequestProperty("contentType", "utf-8");
|
|
|
conn.setDoOutput(true);
|
|
|
conn.setDoInput(true);
|
|
|
+ if (null != map && !map.isEmpty()){
|
|
|
+ for (String key:map.keySet()) {
|
|
|
+ conn.setRequestProperty(key,map.get(key));
|
|
|
+ }
|
|
|
+ }
|
|
|
out = new PrintWriter(conn.getOutputStream());
|
|
|
out.print(param);
|
|
|
out.flush();
|
|
@@ -179,7 +181,7 @@ public class HttpUtils
|
|
|
return result.toString();
|
|
|
}
|
|
|
|
|
|
- public static String sendSSLPost(String url, String param)
|
|
|
+ public static String sendSSLPost(String url, String param, Map<String,String> map)
|
|
|
{
|
|
|
StringBuilder result = new StringBuilder();
|
|
|
String urlNameString = url + "?" + param;
|
|
@@ -195,9 +197,13 @@ public class HttpUtils
|
|
|
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
|
|
conn.setRequestProperty("Accept-Charset", "utf-8");
|
|
|
conn.setRequestProperty("contentType", "utf-8");
|
|
|
+ if (null != map && !map.isEmpty()){
|
|
|
+ for (String key:map.keySet()) {
|
|
|
+ conn.setRequestProperty(key,map.get(key));
|
|
|
+ }
|
|
|
+ }
|
|
|
conn.setDoOutput(true);
|
|
|
conn.setDoInput(true);
|
|
|
-
|
|
|
conn.setSSLSocketFactory(sc.getSocketFactory());
|
|
|
conn.setHostnameVerifier(new TrustAnyHostnameVerifier());
|
|
|
conn.connect();
|