91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Java httpcomponents發送get post請求代碼實例

發布時間:2020-09-28 03:08:01 來源:腳本之家 閱讀:201 作者:賈樹丙 欄目:開發技術

引入的包為:

<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
  <groupId>org.apache.httpcomponents</groupId>
  <artifactId>httpclient</artifactId>
  <version>4.5.8</version>
</dependency>

實現的工具類為:

import com.alibaba.fastjson.JSON;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HttpClientHelper {
  private static Logger logger = LoggerFactory.getLogger(HttpClientHelper.class);

  private HttpClientHelper() {

  }

  /**
   * 發起POST請求
   *
   * @param url   url
   * @param paramMap 參數的Map格式
   */
  public static void sendPost(String url, Map<String, String> paramMap) {
    logger.info("開始發起POST請求,請求地址為{},參數為{}", url, JSON.toJSON(paramMap));
    CloseableHttpResponse response = null;

    try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
      String encoding = "utf-8";
      //創建post請求對象
      HttpPost httpPost = new HttpPost(url);
      //裝填請求參數
      List<NameValuePair> list = new ArrayList<>();
      for (Map.Entry<String, String> entry : paramMap.entrySet()) {
        list.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
      }
      //設置參數到請求對象中
      httpPost.setEntity(new UrlEncodedFormEntity(list, encoding));
      httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");
      httpPost.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
      response = httpClient.execute(httpPost);

    } catch (IOException e) {
      logger.error("POST請求發出失敗,請求的地址為{},參數為{},錯誤信息為{}", url, JSON.toJSON(paramMap), e.getMessage(), e);
    } finally {
      try {
        if (response != null) {
          response.close();
        }
      } catch (IOException e) {
        logger.error("POST請求response關閉異常,錯誤信息為{}", e.getMessage(), e);
      }
    }
  }

  /**
   * 發起GET請求
   *
   * @param urlParam url請求,包含參數
   */
  public static void sendGet(String urlParam) {
    logger.info("開始發起GET請求,請求地址為{}", urlParam);
    HttpGet httpGet = new HttpGet(urlParam);
    CloseableHttpResponse response = null;
    try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
      response = httpClient.execute(httpGet);
      int status = response.getStatusLine().getStatusCode();
      logger.error("GET請求發出成功,請求的地址為{},返回狀態為{}", urlParam, status);
    } catch (IOException e) {
      logger.error("GET請求發出失敗,請求的地址為{},錯誤信息為{}", urlParam, e.getMessage(), e);
    } finally {
      try {
        if (response != null) {
          response.close();
        }
      } catch (IOException e) {
        logger.error("GET請求response關閉異常,錯誤信息為{}", e.getMessage(), e);
      }
    }
  }

  public static void main(String[] args) {
    String url = "https://jiashubing.cn/tencenttest";
    //需要傳入的參數
    Map<String, String> map = new HashMap<>();
    map.put("code", "js");
    map.put("day", "0");
    map.put("city", "北京");
    map.put("dfc", "1");
    map.put("charset", "utf-8");
    sendPost(url, map);


    String urlParam = "https://jiashubing.cn/talk/document?fileid=1234ji賈樹丙";
    sendGet(urlParam);
  }
}

如果POST請求想要發送Json 格式的數據,只需要修改成這樣:

String json = JSON.toJSONString(paramMap);
StringEntity requestEntity = new StringEntity(json, "utf-8");
httpPost.setEntity(requestEntity);

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

张家港市| 隆德县| 高台县| 万山特区| 迁西县| 治县。| 会宁县| 瑞安市| 宁夏| 志丹县| 清镇市| 兴安县| 得荣县| 宿迁市| 田东县| 廉江市| 新源县| 漳州市| 乐东| 奉新县| 南漳县| 盐池县| 马边| 三河市| 南阳市| 樟树市| 泰兴市| 榕江县| 怀集县| 汉寿县| 定远县| 海丰县| 松江区| 衡水市| 疏附县| 随州市| 漠河县| 宁城县| 特克斯县| 中方县| 柳林县|