<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          我是如何實(shí)現(xiàn)HttpGet請求傳body參數(shù)的?

          共 2863字,需瀏覽 6分鐘

           ·

          2022-05-15 00:46

          點(diǎn)擊關(guān)注公眾號,Java干貨及時(shí)送達(dá)

          前言

          最近調(diào)用公司項(xiàng)目一個(gè)接口時(shí),發(fā)現(xiàn)該接口是一個(gè)Get請求,入?yún)⒃贐ody 中(json格式)。

          場景如下:A服務(wù)需發(fā)送http請求調(diào)用B服務(wù)的接口(該接口為Get方式,入?yún)⑹且粋€(gè)json字符串在body中傳遞)

          當(dāng)我看到這個(gè)接口的時(shí)候,感覺好奇怪(MMP,干嘛不用POST請求。Get就get,請求還放Body中,心里有些不爽)盡管心里不爽,但是也只能默默接受,擼起袖子 “干” 就完了!

          實(shí)現(xiàn)過程

          首先官方不推薦這樣做,但是http(基于tcp的超文本傳輸協(xié)議)并沒有規(guī)定,Get 請求不能加body

          一、首先我寫了一個(gè)Get請求接口,本地測試一下,便于大家直觀的理解

          調(diào)用成功:

          本地使用postman調(diào)用是成功的,接下來我們使用Java代碼請求調(diào)用

          二.使用Http工具類調(diào)用Get請求(json參數(shù))
          1. 引入httpclient 依賴

          ?<dependency>
          ?????<groupId>org.apache.httpcomponentsgroupId>
          ?????<artifactId>httpclientartifactId>
          ?????<version>4.5.6version>
          ?dependency>
          1. 定義一個(gè)HttpGet實(shí)體類
          import?org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
          import?java.net.URI;
          /**
          ?*?@author?xf
          ?*?@version?1.0.0
          ?*?@ClassName?HttpGetWithEntity
          ?*?@Description?TODO?定義一個(gè)帶body的GET請求?繼承?HttpEntityEnclosingRequestBase
          ?*/

          public?class?HttpGetWithEntity?extends?HttpEntityEnclosingRequestBase?{
          ????private?final?static?String?METHOD_NAME?=?"GET";

          ????@Override
          ????public?String?getMethod()?{
          ????????return?METHOD_NAME;
          ????}
          ????public?HttpGetWithEntity()?{
          ????????super();
          ????}
          ????public?HttpGetWithEntity(final?URI?uri)?{
          ????????super();
          ????????setURI(uri);
          ????}
          ????HttpGetWithEntity(final?String?uri)?{
          ????????super();
          ????????setURI(URI.create(uri));
          ????}

          }
          1. HttpGet請求公共方法
          ????/**
          ?????*?發(fā)送get請求,參數(shù)為json
          ?????*?@param?url
          ?????*?@param?param
          ?????*?@param?encoding
          ?????*?@return
          ?????*?@throws?Exception
          ?????*/

          ????public?static?String?sendJsonByGetReq(String?url,?String?param,?String?encoding)?throws?Exception?{
          ????????String?body?=?"";
          ????????//創(chuàng)建httpclient對象
          ????????CloseableHttpClient?client?=?HttpClients.createDefault();
          ????????HttpGetWithEntity?httpGetWithEntity?=?new?HttpGetWithEntity(url);
          ????????HttpEntity?httpEntity?=?new?StringEntity(param,?ContentType.APPLICATION_JSON);
          ????????httpGetWithEntity.setEntity(httpEntity);
          ????????//執(zhí)行請求操作,并拿到結(jié)果(同步阻塞)
          ????????CloseableHttpResponse?response?=?client.execute(httpGetWithEntity);
          ????????//獲取結(jié)果實(shí)體
          ????????HttpEntity?entity?=?response.getEntity();
          ????????if?(entity?!=?null)?{
          ????????????//按指定編碼轉(zhuǎn)換結(jié)果實(shí)體為String類型
          ????????????body?=?EntityUtils.toString(entity,?encoding);
          ????????}
          ????????//釋放鏈接
          ????????response.close();
          ????????return?body;
          ????}
          1. 運(yùn)行服務(wù),本地測試調(diào)用一下該接口
          ????/**
          ?????*?測試?Get?請求
          ?????*/

          ????@Test
          ????public?void?test(){
          ????????String?url?=?"http://127.0.0.1:8012/export/getByBodyJson";
          ????????Map?map?=?new?HashMap<>();
          ????????map.put("stuName","張一山");
          ????????map.put("school","北京戲劇學(xué)院");
          ????????String?reqParams?=?JSONArray.toJSON(map).toString();
          ????????try?{
          ????????????String?s?=?sendJsonByGetReq(url,?reqParams,?"UTF-8");
          ????????????System.out.println("請求Get請求返回結(jié)果:"+s);
          ????????}?catch?(Exception?e)?{
          ????????????e.printStackTrace();
          ????????}
          ????}
          三.使用HttpGet請求發(fā)送body入?yún)⒄{(diào)用成功

          盡管這樣解決了get 加body 體傳參,但是仍建議大家使用post 加body!

          來源:admins.blog.csdn.net/article/details/109809386

          ????

          1、拖動(dòng)文件就能觸發(fā)7-Zip安全漏洞,波及所有版本

          2、進(jìn)程切換的本質(zhì)是什么?

          3、一次 SQL 查詢優(yōu)化原理分析:900W+ 數(shù)據(jù),從 17s 到 300ms

          4、Redis數(shù)據(jù)結(jié)構(gòu)為什么既省內(nèi)存又高效?

          5、IntelliJ IDEA快捷鍵大全 + 動(dòng)圖演示

          6、全球第三瀏覽器,封殺中國用戶這種操作?。ㄎ哪┧蜁?/a>

          點(diǎn)

          點(diǎn)

          點(diǎn)點(diǎn)

          點(diǎn)

          瀏覽 77
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評論
          圖片
          表情
          推薦
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  深夜操逼| 操逼姐妹双飞 | 日本手机在线播放 | 欧美成人777 | 必死交尾在线新日韩 |