基於HttpClient的Http請求工具類

僅參考, 基於HttpClient 的Http請求工具類

public class HttpClientUtil {


   private static Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);

   public static String doGet(String url, Map<String, String> param) {
      // 創建http對象
      CloseableHttpClient httpclient = HttpClients.createDefault();
      CloseableHttpResponse response = null;
      String  resultString = "";
      try {
         // 組裝URL===start
         URIBuilder builder = new URIBuilder(url);
         if (param != null) {
            for (String key : param.keySet()) {
               builder.addParameter(key, param.get(key));
            }
         }
         URI uri = builder.build();
         // 組裝URL===end
         HttpGet httpGet = new HttpGet(uri);// 創建請求對象
         response = httpclient.execute(httpGet);// 發送請求
         if (response.getStatusLine().getStatusCode() == 200) {// 判斷返回狀態是否爲200
              resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
         }
      } catch (Exception e) {
         e.printStackTrace();
      } finally {
         try {
            if (response != null) {
               response.close();
            }
            httpclient.close();
         } catch (IOException e) {
            e.printStackTrace();
         }
      }
      return resultString;
   }

   public static String doGet(String url) {
      return doGet(url, null);
   }

   public static String doPost(String url, Map<String, String> param) {
        logger.info("doPost, url:{}, param:{}",url, JSONUtils.toJSONString(param));
      // 創建Httpclient對象
      CloseableHttpClient httpClient = HttpClients.createDefault();
      CloseableHttpResponse response = null;
      String resultString = "";
      try {
         // 創建Http Post請求
         HttpPost httpPost = new HttpPost(url);
         // 創建參數列表
         if (param != null) {
            List<NameValuePair> paramList = new ArrayList<>();
            for (String key : param.keySet()) {
               paramList.add(new BasicNameValuePair(key, param.get(key)));
            }
            // 模擬表單
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList, HTTP.UTF_8);
            httpPost.setEntity(entity);
         }
         // 執行http請求
         response = httpClient.execute(httpPost);
         resultString = EntityUtils.toString(response.getEntity(), "utf-8");
      } catch (Exception e) {
         e.printStackTrace();
      } finally {
         try {
            response.close();
         } catch (IOException e) {
                logger.info("doPost param IOException", e);
            e.printStackTrace();
         }
      }

      return resultString;
   }

   public static String doPost(String url) {
      return doPost(url, null);
   }
   
   public static String doPostJson(String url, String json) {
      // 創建Httpclient對象
      CloseableHttpClient httpClient = HttpClients.createDefault();
      CloseableHttpResponse response = null;
      String resultString = "";
      try {
         // 創建Http Post請求
         HttpPost httpPost = new HttpPost(url);
         // 創建請求內容
         StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
         httpPost.setEntity(entity);
         // 執行http請求
         response = httpClient.execute(httpPost);
         resultString = EntityUtils.toString(response.getEntity(), "utf-8");
      } catch (Exception e) {
         e.printStackTrace();
      } finally {
         try {
            response.close();
         } catch (IOException e) {
                logger.info("doPost json IOException", e);
            e.printStackTrace();
         }
      }

      return resultString;
   }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章