|
@@ -16,6 +16,8 @@ import java.security.KeyManagementException;
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
import java.security.cert.CertificateException;
|
|
|
import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Iterator;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.Map.Entry;
|
|
@@ -26,6 +28,7 @@ import javax.net.ssl.TrustManager;
|
|
|
import javax.net.ssl.X509TrustManager;
|
|
|
|
|
|
import org.apache.http.Consts;
|
|
|
+import org.apache.http.Header;
|
|
|
import org.apache.http.HttpEntity;
|
|
|
import org.apache.http.HttpResponse;
|
|
|
import org.apache.http.NameValuePair;
|
|
@@ -110,8 +113,8 @@ public class HttpUtil {
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
- public static Response sendGetRequest(String url, Map<String, String> params) throws Exception {
|
|
|
- return sendGetRequest(url, params, false, null);
|
|
|
+ public static Response sendGetRequest(String url, HashMap<String, String> header, Map<String, String> params) throws Exception {
|
|
|
+ return sendGetRequest(url, header, params, false, null);
|
|
|
}
|
|
|
|
|
|
|
|
@@ -124,8 +127,8 @@ public class HttpUtil {
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
- public static Response sendGetRequest(String url, Map<String, String> params, boolean sign, String signKey) throws Exception {
|
|
|
- return sendRequest(RequestMethod.GET, url, params, sign, signKey);
|
|
|
+ public static Response sendGetRequest(String url, HashMap<String, String> header, Map<String, String> params, boolean sign, String signKey) throws Exception {
|
|
|
+ return sendRequest(RequestMethod.GET, url, header, params, sign, signKey);
|
|
|
}
|
|
|
|
|
|
|
|
@@ -138,8 +141,8 @@ public class HttpUtil {
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
- public static Response sendGetRequest(String url, Map<String, String> params, boolean sign) throws Exception {
|
|
|
- return sendRequest(RequestMethod.GET, url, params, sign, null);
|
|
|
+ public static Response sendGetRequest(String url, HashMap<String, String> header, Map<String, String> params, boolean sign) throws Exception {
|
|
|
+ return sendRequest(RequestMethod.GET, url, header, params, sign, null);
|
|
|
}
|
|
|
|
|
|
|
|
@@ -150,8 +153,8 @@ public class HttpUtil {
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
- public static Response sendPostRequest(String url, Map<String, String> params) throws Exception {
|
|
|
- return sendPostRequest(url, params, false, null);
|
|
|
+ public static Response sendPostRequest(String url, HashMap<String, String> header, Map<String, String> params) throws Exception {
|
|
|
+ return sendPostRequest(url, header, params, false, null);
|
|
|
}
|
|
|
|
|
|
|
|
@@ -176,8 +179,8 @@ public class HttpUtil {
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
- public static Response sendPostRequest(String url, Map<String, String> params, boolean sign, String signKey) throws Exception {
|
|
|
- return sendRequest(RequestMethod.POST, url, params, sign, signKey);
|
|
|
+ public static Response sendPostRequest(String url, HashMap<String, String> header, Map<String, String> params, boolean sign, String signKey) throws Exception {
|
|
|
+ return sendRequest(RequestMethod.POST, url, header, params, sign, signKey);
|
|
|
}
|
|
|
|
|
|
|
|
@@ -208,8 +211,8 @@ public class HttpUtil {
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
- public static Response sendPostRequest(String url, Map<String, String> params, boolean sign) throws Exception {
|
|
|
- return sendRequest(RequestMethod.POST, url, params, sign, null);
|
|
|
+ public static Response sendPostRequest(String url, HashMap<String, String> header, Map<String, String> params, boolean sign) throws Exception {
|
|
|
+ return sendRequest(RequestMethod.POST, url, header, params, sign, null);
|
|
|
}
|
|
|
|
|
|
|
|
@@ -350,8 +353,8 @@ public class HttpUtil {
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
- public static Response sendPutRequest(String url, Map<String, String> params, boolean sign) throws Exception {
|
|
|
- return sendRequest(RequestMethod.PUT, url, params, sign, null);
|
|
|
+ public static Response sendPutRequest(String url, HashMap<String, String> header, Map<String, String> params, boolean sign) throws Exception {
|
|
|
+ return sendRequest(RequestMethod.PUT, url, header, params, sign, null);
|
|
|
}
|
|
|
|
|
|
|
|
@@ -389,8 +392,8 @@ public class HttpUtil {
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
- public static Response sendDeleteRequest(String url, Map<String, String> params) throws Exception {
|
|
|
- return sendDeleteRequest(url, params, false, null);
|
|
|
+ public static Response sendDeleteRequest(String url, HashMap<String, String> header, Map<String, String> params) throws Exception {
|
|
|
+ return sendDeleteRequest(url, header, params, false, null);
|
|
|
}
|
|
|
|
|
|
|
|
@@ -403,8 +406,8 @@ public class HttpUtil {
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
- public static Response sendDeleteRequest(String url, Map<String, String> params, boolean sign, String signKey) throws Exception {
|
|
|
- return sendRequest(RequestMethod.DELETE, url, params, sign, signKey);
|
|
|
+ public static Response sendDeleteRequest(String url, HashMap<String, String> header, Map<String, String> params, boolean sign, String signKey) throws Exception {
|
|
|
+ return sendRequest(RequestMethod.DELETE, url, header, params, sign, signKey);
|
|
|
}
|
|
|
|
|
|
|
|
@@ -417,8 +420,8 @@ public class HttpUtil {
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
- public static Response sendDeleteRequest(String url, Map<String, String> params, boolean sign) throws Exception {
|
|
|
- return sendRequest(RequestMethod.DELETE, url, params, sign, null);
|
|
|
+ public static Response sendDeleteRequest(String url, HashMap<String, String> header, Map<String, String> params, boolean sign) throws Exception {
|
|
|
+ return sendRequest(RequestMethod.DELETE, url, header, params, sign, null);
|
|
|
}
|
|
|
|
|
|
|
|
@@ -430,24 +433,51 @@ public class HttpUtil {
|
|
|
* 请求链接
|
|
|
* @param params
|
|
|
* 参数
|
|
|
+ *
|
|
|
* @param sign
|
|
|
* 是否签名
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
- public static Response sendRequest(RequestMethod method, String url, Map<String, String> params, boolean sign, String signKey)
|
|
|
+ public static Response sendRequest(RequestMethod method, String url, HashMap<String, String> header, Map<String, String> params, boolean sign, String signKey)
|
|
|
throws Exception {
|
|
|
+
|
|
|
switch (method) {
|
|
|
- case GET:
|
|
|
- return sendHttpUriRequest(new HttpGet(getRequestUrl(url, params, sign, signKey)));
|
|
|
- case POST:
|
|
|
- return sendHttpEntityEnclosingRequest(new HttpPost(getRequestUrl(url, sign, signKey)), params);
|
|
|
- case PUT:
|
|
|
- return sendHttpEntityEnclosingRequest(new HttpPut(getRequestUrl(url, sign, signKey)), params);
|
|
|
- case DELETE:
|
|
|
- return sendHttpUriRequest(new HttpDelete(getRequestUrl(url, params, sign, signKey)));
|
|
|
- default:
|
|
|
- return sendHttpUriRequest(new HttpGet(getRequestUrl(url, params, sign, signKey)));
|
|
|
+ case GET: {
|
|
|
+ HttpRequestBase request = new HttpGet(getRequestUrl(url, params, sign, signKey));
|
|
|
+ for (Map.Entry<String, String> entry : header.entrySet()) {
|
|
|
+ request.setHeader(entry.getKey(), entry.getValue());
|
|
|
+ }
|
|
|
+ return sendHttpUriRequest(request);
|
|
|
+ }
|
|
|
+ case POST: {
|
|
|
+ HttpPost request = new HttpPost(getRequestUrl(url, sign, signKey));
|
|
|
+ for (Map.Entry<String, String> entry : header.entrySet()) {
|
|
|
+ request.setHeader(entry.getKey(), entry.getValue());
|
|
|
+ }
|
|
|
+ return sendHttpEntityEnclosingRequest(request, params);
|
|
|
+ }
|
|
|
+ case PUT: {
|
|
|
+ HttpPut request = new HttpPut(getRequestUrl(url, sign, signKey));
|
|
|
+ for (Map.Entry<String, String> entry : header.entrySet()) {
|
|
|
+ request.setHeader(entry.getKey(), entry.getValue());
|
|
|
+ }
|
|
|
+ return sendHttpEntityEnclosingRequest(request, params);
|
|
|
+ }
|
|
|
+ case DELETE: {
|
|
|
+ HttpDelete request = new HttpDelete(getRequestUrl(url, params, sign, signKey));
|
|
|
+ for (Map.Entry<String, String> entry : header.entrySet()) {
|
|
|
+ request.setHeader(entry.getKey(), entry.getValue());
|
|
|
+ }
|
|
|
+ return sendHttpUriRequest(request);
|
|
|
+ }
|
|
|
+ default: {
|
|
|
+ HttpGet request = new HttpGet(getRequestUrl(url, params, sign, signKey));
|
|
|
+ for (Map.Entry<String, String> entry : header.entrySet()) {
|
|
|
+ request.setHeader(entry.getKey(), entry.getValue());
|
|
|
+ }
|
|
|
+ return sendHttpUriRequest(request);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|