|
|
@@ -0,0 +1,82 @@
|
|
|
+package com.goafanti.common.utils;
|
|
|
+
|
|
|
+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;
|
|
|
+import org.apache.http.client.ClientProtocolException;
|
|
|
+import org.apache.http.client.HttpClient;
|
|
|
+import org.apache.http.client.methods.HttpGet;
|
|
|
+import org.apache.http.client.methods.HttpPost;
|
|
|
+import org.apache.http.entity.StringEntity;
|
|
|
+import org.apache.http.impl.client.HttpClientBuilder;
|
|
|
+import org.apache.http.util.EntityUtils;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.nio.charset.Charset;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+public class HttpUtils {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public static JSONObject httpGet(String url) {
|
|
|
+ JSONObject jsonResult = null;
|
|
|
+ try {
|
|
|
+ HttpClient client = HttpClientBuilder.create().build();//获取DefaultHttpClient请求
|
|
|
+ HttpGet request = new HttpGet(url);
|
|
|
+ HttpResponse response = client.execute(request);
|
|
|
+ if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
|
|
|
+ String strResult = EntityUtils.toString(response.getEntity(),"UTF-8");//此处设定编码格式
|
|
|
+ jsonResult = JSON.parseObject(strResult);
|
|
|
+ } else {
|
|
|
+ LoggerUtils.error(HttpUtils.class,"HttpStatus异常");
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return jsonResult;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * @param url 地址
|
|
|
+ * @param map 参数
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static JSONObject httpPost(String url,Map<String, Object>map){
|
|
|
+ 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")));
|
|
|
+ HttpResponse response = null;
|
|
|
+ try {
|
|
|
+ response = httpClient.execute(httpPost);
|
|
|
+ if (null == response || response.getStatusLine() == null) {
|
|
|
+ throw new Exception("Post Request For Url[{}] is not ok. Response is null");
|
|
|
+ } else if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
|
|
|
+ throw new Exception("Post Request For Url[{}] is not ok. Response Status Code is {"+response.getStatusLine().getStatusCode()+"}");
|
|
|
+ }
|
|
|
+ } catch (ClientProtocolException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ String resultString=null;
|
|
|
+ try {
|
|
|
+ resultString = EntityUtils.toString(response.getEntity());
|
|
|
+ } catch (ParseException | IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ JSONObject jsonObj = JSONObject.parseObject(resultString);
|
|
|
+ return jsonObj;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|