java開發(fā)hutool HttpUtil網(wǎng)絡(luò)請(qǐng)求工具使用demo
一、測(cè)試代碼
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("請(qǐng)求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è)置請(qǐng)求體參數(shù) String requestBody = "{\"param1\": \"value1\", \"param2\": \"value2\"}"; httpRequest.body(requestBody) .setHeader("Content-Type", "application/json") .timeout(20000); // 設(shè)置超時(shí)時(shí)間為20秒 // 設(shè)置請(qǐng)求參數(shù) httpRequest.setQueryParam("param1", "value1") .setHeader("User-Agent", "Hutool") .timeout(20000); // 設(shè)置超時(shí)時(shí)間為20秒
以上就是java開發(fā)hutool HttpUtil網(wǎng)絡(luò)請(qǐng)求工具使用demo的詳細(xì)內(nèi)容,更多關(guān)于hutool HttpUtil網(wǎng)絡(luò)請(qǐng)求的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
關(guān)于Mybatis 中使用Mysql存儲(chǔ)過(guò)程的方法
這篇文章給大家介紹了Mybatis 中使用Mysql存儲(chǔ)過(guò)程的方法,本文通過(guò)實(shí)例代碼相結(jié)合的形式給大家介紹的非常詳細(xì),具有參考借鑒價(jià)值,需要的朋友參考下吧2018-03-03java讀取圖片并轉(zhuǎn)化為二進(jìn)制字符串的實(shí)現(xiàn)方法
這篇文章主要介紹了java讀取圖片并轉(zhuǎn)化為二進(jìn)制字符串的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-09-09Java實(shí)現(xiàn)獲取指定個(gè)數(shù)的不同隨機(jī)數(shù)
今天小編就為大家分享一篇關(guān)于Java實(shí)現(xiàn)獲取指定個(gè)數(shù)的不同隨機(jī)數(shù),小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-01-01解決spring-boot-maven-plugin報(bào)紅的問(wèn)題
這篇文章主要給大家介紹一下如何解決spring-boot-maven-plugin報(bào)紅的問(wèn)題,文中通過(guò)圖文講解的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下2023-08-08基于Java Callable接口實(shí)現(xiàn)線程代碼實(shí)例
這篇文章主要介紹了基于Java Callable接口實(shí)現(xiàn)線程代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08