|
|
@@ -1,9 +1,7 @@
|
|
|
package com.goafanti.common.utils;
|
|
|
|
|
|
-import java.io.IOException;
|
|
|
-import java.nio.charset.Charset;
|
|
|
-import java.util.Map;
|
|
|
-
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import org.apache.http.HttpResponse;
|
|
|
import org.apache.http.HttpStatus;
|
|
|
import org.apache.http.ParseException;
|
|
|
@@ -15,13 +13,15 @@ import org.apache.http.entity.StringEntity;
|
|
|
import org.apache.http.impl.client.HttpClientBuilder;
|
|
|
import org.apache.http.util.EntityUtils;
|
|
|
|
|
|
-import com.alibaba.fastjson.JSON;
|
|
|
-import com.alibaba.fastjson.JSONObject;
|
|
|
+import java.io.IOException;
|
|
|
+import java.nio.charset.Charset;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Set;
|
|
|
|
|
|
public class HttpUtils {
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
public static JSONObject httpGet(String url) {
|
|
|
JSONObject jsonResult = null;
|
|
|
try {
|
|
|
@@ -39,19 +39,33 @@ public class HttpUtils {
|
|
|
}
|
|
|
return jsonResult;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
+ public static JSONObject httpPost(String url,Map<String, Object>map){
|
|
|
+ return httpPost(url,map,0);
|
|
|
+ }
|
|
|
+ public static JSONObject httpPostBody(String url,Map<String, Object>map){
|
|
|
+ return httpPost(url,map,1);
|
|
|
+ }
|
|
|
/**
|
|
|
- *
|
|
|
+ *
|
|
|
* @param url 地址
|
|
|
* @param map 参数
|
|
|
+ * @param type 0 json 1body
|
|
|
* @return
|
|
|
*/
|
|
|
- public static JSONObject httpPost(String url,Map<String, Object>map){
|
|
|
+ public static JSONObject httpPost(String url,Map<String, Object>map,Integer type) {
|
|
|
HttpClient httpClient = HttpClientBuilder.create().build();
|
|
|
HttpPost httpPost = new HttpPost(url);
|
|
|
- httpPost.addHeader("Content-type", "application/json");
|
|
|
httpPost.setHeader("Accept", "application/json");
|
|
|
- httpPost.setEntity(new StringEntity(JSON.toJSONString(map), Charset.forName("UTF-8")));
|
|
|
+ if (type==0){
|
|
|
+ httpPost.addHeader("Content-type", "application/json");
|
|
|
+ httpPost.setEntity(new StringEntity(JSON.toJSONString(map), Charset.forName("UTF-8")));
|
|
|
+ }else{
|
|
|
+ httpPost.addHeader("Content-type", "application/x-www-form-urlencoded");
|
|
|
+ httpPost.setEntity(new StringEntity(ParamMap(map), Charset.forName("UTF-8")));
|
|
|
+ }
|
|
|
+
|
|
|
HttpResponse response = null;
|
|
|
try {
|
|
|
response = httpClient.execute(httpPost);
|
|
|
@@ -77,7 +91,16 @@ public class HttpUtils {
|
|
|
return jsonObj;
|
|
|
}
|
|
|
|
|
|
+ private static String ParamMap(Map<String, Object> map) {
|
|
|
+ StringBuffer str =new StringBuffer();
|
|
|
+ Set<String> strings = map.keySet();
|
|
|
+ for (String string : strings) {
|
|
|
+ str=str.append(string).append("=").append(map.get(string)).append("&");
|
|
|
+ }
|
|
|
+ String substring = str.substring(0, str.length() - 1);
|
|
|
+ System.out.println(substring);
|
|
|
+ return substring;
|
|
|
+ }
|
|
|
|
|
|
-
|
|
|
|
|
|
}
|