最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java開發(fā)hutool HttpUtil網(wǎng)絡(luò)請求工具使用demo

 更新時間:2023年07月09日 14:31:00   作者:AC編程  
這篇文章主要為大家介紹了hutool之HttpUtil網(wǎng)絡(luò)請求工具使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

一、測試代碼

import cn.hutool.core.text.UnicodeUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.google.gson.Gson;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import java.util.HashMap;
import java.util.Map;
@Slf4j
public class Test {
    public static void main(String[] args) {
        requestToken();
    }
    public static String requestToken() {
        try {
            String url = "https://alanchen.com/auth/getToken/V2";
            HttpRequest request = HttpUtil.createPost(url);
            request.header("PartnerCode", "testCode");
            Map<String, String> param = new HashMap();
            param.put("partnerCode", "testCode");
            param.put("partnerSecret", "secret");
            Gson gson = new Gson();
            String body = gson.toJson(param);
            request.body(body);
            HttpResponse execute = request.execute();
            if (!execute.isOk()) {
                log.error("請求token失敗,body={},execute={}", execute.body(), execute);
                throw new RuntimeException(execute.body());
            }
            String res = UnicodeUtil.toString(execute.body());
            JSONObject jsonObject = JSONUtil.parseObj(res, true);
            AuthTokenResResult resultObj = jsonObject.toBean(AuthTokenResResult.class, true);
            log.info("requestToken,resultObj={}", resultObj);
            if (resultObj.getCode() != 200) {
                log.error("獲取token失敗,code={},msg={},result={}", resultObj.getCode(), resultObj.getMsg(), resultObj);
                throw new RuntimeException("獲取token失敗,code=" + resultObj.getCode() + ",msg=" + resultObj.getMsg());
            }
            if (resultObj.getData() == null) {
                log.error("獲取token為空,code={},msg={},result={}", resultObj.getCode(), resultObj.getMsg(), resultObj);
                throw new RuntimeException("獲取token為空,code=" + resultObj.getCode() + ",msg=" + resultObj.getMsg());
            }
            return resultObj.getData().getToken();
        } catch (Exception e) {
            log.error("requestToken失敗,msg={}", e.getMessage());
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }
    @Data
    public class AuthTokenResResult {
        private int code;
        private String msg;
        private AuthToken data;
        private long count;
    }
    @Data
    public class AuthToken {
        private String token;
        private long expireAt;
    }
}

二、代碼片段

 // 設(shè)置請求體參數(shù)
String requestBody = "{\"param1\": \"value1\", \"param2\": \"value2\"}";
httpRequest.body(requestBody)
        .setHeader("Content-Type", "application/json")
        .timeout(20000); // 設(shè)置超時時間為20秒
// 設(shè)置請求參數(shù)
httpRequest.setQueryParam("param1", "value1")
          .setHeader("User-Agent", "Hutool")
          .timeout(20000); // 設(shè)置超時時間為20秒

以上就是java開發(fā)hutool HttpUtil網(wǎng)絡(luò)請求工具使用demo的詳細內(nèi)容,更多關(guān)于hutool HttpUtil網(wǎng)絡(luò)請求的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 一文帶你搞懂SpringBoot中自動裝配原理

    一文帶你搞懂SpringBoot中自動裝配原理

    這篇文章主要為大家詳細介紹了SpringBoot中自動裝配原理的相關(guān)知識,文中的示例代碼講解詳細,具有一定的借鑒價值,有需要的小伙伴可以參考下
    2025-01-01
  • springboot 項目使用jasypt加密數(shù)據(jù)源的方法

    springboot 項目使用jasypt加密數(shù)據(jù)源的方法

    Jasypt 是一個 Java 庫,它允許開發(fā)者以最小的努力為他/她的項目添加基本的加密功能,而且不需要對密碼學(xué)的工作原理有深刻的了解。接下來通過本文給大家介紹springboot 項目使用jasypt加密數(shù)據(jù)源的問題,一起看看吧
    2021-11-11
  • Spring Boot 的注解生效詳細步驟解析

    Spring Boot 的注解生效詳細步驟解析

    Spring通過掃描、解析、注冊分層處理配置類注解(如@Configuration、@ComponentScan等),利用遞歸、延遲加載和緩存機制,實現(xiàn)BeanDefinition的動態(tài)注冊與靈活擴展,解決依賴和性能問題,本文給大家介紹Spring Boot的注解生效詳細步驟解析,感興趣的朋友跟隨小編一起看看吧
    2025-09-09
  • Java中的單向鏈表詳解

    Java中的單向鏈表詳解

    這篇文章主要介紹了Java中的單向鏈表詳解,單向鏈表又叫單鏈表,是鏈表的一種,由節(jié)點構(gòu)成,head指針指向第一個稱為表頭節(jié)點,而終止指向最后一個null指針,需要的朋友可以參考下
    2024-01-01
  • Hibernate處理多對多關(guān)系的實現(xiàn)示例

    Hibernate處理多對多關(guān)系的實現(xiàn)示例

    本文介紹了Hibernate中實現(xiàn)多對多關(guān)系的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2026-01-01
  • SpringBoot集成使用Redis及搭建過程

    SpringBoot集成使用Redis及搭建過程

    jackson-json 工具提供了 javabean 與 json 之 間的轉(zhuǎn)換能力,可以將 pojo 實例序列化成 json 格式存儲在 redis 中,也可以將 json 格式的數(shù)據(jù)轉(zhuǎn)換成 pojo 實例,本文給大家介紹SpringBoot集成使用Redis及搭建過程,感興趣的朋友一起看看吧
    2022-01-01
  • Java使用itextpdf實現(xiàn)表單導(dǎo)出為pdf

    Java使用itextpdf實現(xiàn)表單導(dǎo)出為pdf

    這篇文章主要為大家詳細介紹了Java如何使用itextpdf實現(xiàn)form表單導(dǎo)出為pdf,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2025-06-06
  • SpringBoot3.0+SpringSecurity6.0+JWT的實現(xiàn)

    SpringBoot3.0+SpringSecurity6.0+JWT的實現(xiàn)

    本文主要介紹了SpringBoot3.0+SpringSecurity6.0+JWT的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-11-11
  • Mybatis獲取參數(shù)值和查詢功能的案例詳解

    Mybatis獲取參數(shù)值和查詢功能的案例詳解

    這篇文章主要介紹了Mybatis獲取參數(shù)值和查詢功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-03-03
  • Spring中@Autowired自動注入map詳解

    Spring中@Autowired自動注入map詳解

    這篇文章主要介紹了Spring中@Autowired自動注入map詳解,  spring是支持基于接口實現(xiàn)類的直接注入的,支持注入map,list等集合中,不用做其他的配置,直接注入,需要的朋友可以參考下
    2023-10-10

最新評論

津市市| 石景山区| 怀安县| 元谋县| 鹤岗市| 南昌县| 黔西县| 响水县| 永泰县| 江都市| 化德县| 阿城市| 临泽县| 萨迦县| 尼玛县| 沧州市| 喀喇沁旗| 民乐县| 富平县| 农安县| 黄陵县| 河南省| 峨眉山市| 九江县| 灵台县| 赤城县| 景泰县| 赫章县| 姜堰市| 河源市| 白沙| 天津市| 连平县| 施甸县| 棋牌| 大英县| 枝江市| 乐都县| 清河县| 洛川县| 观塘区|