溫馨提示×

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

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

Apache中怎么實(shí)現(xiàn)一個(gè)httpclient封裝類(lèi)

發(fā)布時(shí)間:2021-08-03 15:03:21 來(lái)源:億速云 閱讀:177 作者:Leah 欄目:編程語(yǔ)言

Apache中怎么實(shí)現(xiàn)一個(gè)httpclient封裝類(lèi),很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。

基于apache httpcomponents組件,首先依賴(lài)jar包

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.13</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpmime</artifactId>
    <version>4.5.13</version>
</dependency>

先來(lái)看使用方法:

/**
 * @author tanghc
 */
public class HttpTest {


    /**
     * 普通get請(qǐng)求
     * @throws IOException
     */
    @Test
    public void get() throws IOException {
        String s = HttpHelper.get("http://localhost:8089/http/get")
                // 添加請(qǐng)求參數(shù)
                .parameter("id", "1")
                .parameter("name", "jim")
                .execute()
                .asString();
        System.out.println(s);
    }

    /**
     * post提交表單
     * @throws IOException
     */
    @Test
    public void postForm() throws IOException {
        Map<String, Object> form = new HashMap<>();
        form.put("id", 1);
        form.put("name", "tom");
        String s = HttpHelper.postForm("http://localhost:8089/http/postForm", form)
                .execute()
                .asString();
        System.out.println(s);
    }

    /**
     * post提交表單,并上傳文件。multipart
     * @throws IOException
     */
    @Test
    public void postFormFile() throws IOException {
        Map<String, Object> form = new HashMap<>();
        HttpHelper.UploadFile file = new HttpHelper.UploadFile("file", "a.txt", "hello world".getBytes());
        form.put("id", 1);
        form.put("name", "tom");
        String s = HttpHelper.postForm("http://localhost:8089/http/postFormFile", form, file)
                .execute()
                .asString();
        System.out.println(s);
    }

    /**
     * post提交json
     * @throws IOException
     */
    @Test
    public void postJson() throws IOException {
        Map<String, Object> form = new HashMap<>();
        form.put("id", 1);
        form.put("name", "tom");
        String json = JSON.toJSONString(form);
        String s = HttpHelper.postJson("http://localhost:8089/http/postJson", json)
                .execute()
                .asString();
        System.out.println(s);
    }

    /**
     * 設(shè)置header
     * @throws IOException
     */
    @Test
    public void setHeader() throws IOException {
        String s = HttpHelper.get("http://localhost:8089/http/header")
                // 設(shè)置單個(gè)header
                .header("token", "xxx")
                .execute()
                .asString();
        System.out.println(s);

        Map<String, String> headers = new HashMap<>();
        headers.put("token", "bbbb");
        String s2 = HttpHelper.get("http://localhost:8089/http/header")
                // 設(shè)置全部header
                .headers(headers)
                .execute()
                .asString();
        System.out.println(s2);
    }

    /**
     * 返回流
     * @throws IOException
     */
    @Test
    public void getStream() throws IOException {
        InputStream inputStream = HttpHelper.get("http://localhost:8089/http/stream")
                .execute()
                .asStream();
        String s = IOUtils.toString(inputStream);
        System.out.println(s);
    }

    /**
     * 獲取http狀態(tài)
     * @throws IOException
     */
    @Test
    public void getStatus() throws IOException {
        HttpHelper.ResponseResult responseResult = HttpHelper.get("http://localhost:8089/http/get")
                .parameter("id", "1")
                .parameter("name", "jim")
                .execute();
        int status = responseResult.getStatus();
        if (status == HttpStatus.SC_OK) {
            System.out.println(responseResult.asString());
        }
    }

    /**
     * 獲取響應(yīng)頭
     * @throws IOException
     */
    @Test
    public void getHeaders() throws IOException {
        Map<String, Object> form = new HashMap<>();
        form.put("id", 1);
        form.put("name", "tom");
        String json = JSON.toJSONString(form);
        HttpHelper.ResponseResult responseResult = HttpHelper.postJson("http://localhost:8089/http/responseHeader", json)
                .execute();
        Map<String, String> headers = responseResult.getHeaders();
        System.out.println(headers.get("code"));
        System.out.println(headers.get("token"));
        System.out.println(responseResult.asString());
    }

    /**
     * 獲取HttpResponse對(duì)象
     * @throws IOException
     */
    @Test
    public void getResponse() throws IOException {
        HttpHelper.ResponseResult responseResult = HttpHelper.get("http://localhost:8089/http/get")
                .parameter("id", "1")
                .parameter("name", "jim")
                .execute();
        HttpResponse response = responseResult.getResponse();
        Locale locale = response.getLocale();
        System.out.println(locale);
    }

    /**
     * 自定義
     * @throws IOException
     */
    @Test
    public void custom() throws IOException {
        String s = HttpHelper.create()
                .url("http://localhost:8089/http/get")
                .method("get")
                .parameter("id", "1")
                .parameter("name", "jim")
                .header("token", "xx")
                .execute()
                .asString();
        System.out.println(s);

        String json = "{ \"name\": \"Jim\" }";
        String s2 = HttpHelper.create()
                .url("http://localhost:8089/http/postJson")
                .method("POST")
                .header("token", "xx")
                .entity(new StringEntity(json, ContentType.APPLICATION_JSON))
                .execute()
                .asString();
        System.out.println(s2);
    }

}

看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝您對(duì)億速云的支持。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI