欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java實(shí)現(xiàn)HttpGet請(qǐng)求傳body參數(shù)

 更新時(shí)間:2024年02月02日 08:38:26   作者:余生一個(gè)帆  
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)HttpGet請(qǐng)求傳body參數(shù)的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

前言

最近調(diào)用公司項(xiàng)目一個(gè)接口時(shí),發(fā)現(xiàn)該接口是一個(gè)Get請(qǐng)求,入?yún)⒃贐ody 中(json格式)。場(chǎng)景如下:A服務(wù)需發(fā)送http請(qǐng)求調(diào)用B服務(wù)的接口(該接口為Get方式,入?yún)⑹且粋€(gè)json字符串在body中傳遞)

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

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

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

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

調(diào)用成功:

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

二.使用Http工具類調(diào)用Get請(qǐng)求(json參數(shù))

1.引入httpclient 依賴

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

2.定義一個(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請(qǐng)求 繼承 HttpEntityEnclosingRequestBase
 * @createTime 2020.11.18 13:51
 */
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));
    }

}

3.HttpGet請(qǐng)求公共方法

    /**
     * 發(fā)送get請(qǐng)求,參數(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對(duì)象
        CloseableHttpClient client = HttpClients.createDefault();
        HttpGetWithEntity httpGetWithEntity = new HttpGetWithEntity(url);
        HttpEntity httpEntity = new StringEntity(param, ContentType.APPLICATION_JSON);
        httpGetWithEntity.setEntity(httpEntity);
        //執(zhí)行請(qǐng)求操作,并拿到結(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;
    }

4.運(yùn)行服務(wù),本地測(cè)試調(diào)用一下該接口

    /**
     * 測(cè)試 Get 請(qǐng)求
     */
    @Test
    public void test(){
        String url = "http://127.0.0.1:8012/export/getByBodyJson";
        Map<String, Object> 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("請(qǐng)求Get請(qǐng)求返回結(jié)果:"+s);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

三.使用HttpGet請(qǐng)求發(fā)送body入?yún)⒄{(diào)用成功

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

到此這篇關(guān)于Java實(shí)現(xiàn)HttpGet請(qǐng)求傳body參數(shù)的文章就介紹到這了,更多相關(guān)Java HttpGet請(qǐng)求傳body參數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論