java開發(fā)hutool HttpUtil網(wǎng)絡請求工具使用demo
更新時間:2023年07月09日 14:31:00 作者:AC編程
這篇文章主要為大家介紹了hutool之HttpUtil網(wǎng)絡請求工具使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
一、測試代碼
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ù) String requestBody = "{\"param1\": \"value1\", \"param2\": \"value2\"}"; httpRequest.body(requestBody) .setHeader("Content-Type", "application/json") .timeout(20000); // 設置超時時間為20秒 // 設置請求參數(shù) httpRequest.setQueryParam("param1", "value1") .setHeader("User-Agent", "Hutool") .timeout(20000); // 設置超時時間為20秒
以上就是java開發(fā)hutool HttpUtil網(wǎng)絡請求工具使用demo的詳細內(nèi)容,更多關于hutool HttpUtil網(wǎng)絡請求的資料請關注腳本之家其它相關文章!
相關文章
java讀取圖片并轉(zhuǎn)化為二進制字符串的實現(xiàn)方法
這篇文章主要介紹了java讀取圖片并轉(zhuǎn)化為二進制字符串的實例代碼,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-09-09Java實現(xiàn)獲取指定個數(shù)的不同隨機數(shù)
今天小編就為大家分享一篇關于Java實現(xiàn)獲取指定個數(shù)的不同隨機數(shù),小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-01-01解決spring-boot-maven-plugin報紅的問題
這篇文章主要給大家介紹一下如何解決spring-boot-maven-plugin報紅的問題,文中通過圖文講解的非常詳細,具有一定的參考價值,需要的朋友可以參考下2023-08-08基于Java Callable接口實現(xiàn)線程代碼實例
這篇文章主要介紹了基于Java Callable接口實現(xiàn)線程代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-08-08