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 項目使用jasypt加密數(shù)據(jù)源的方法
Jasypt 是一個 Java 庫,它允許開發(fā)者以最小的努力為他/她的項目添加基本的加密功能,而且不需要對密碼學(xué)的工作原理有深刻的了解。接下來通過本文給大家介紹springboot 項目使用jasypt加密數(shù)據(jù)源的問題,一起看看吧2021-11-11
Hibernate處理多對多關(guān)系的實現(xiàn)示例
本文介紹了Hibernate中實現(xiàn)多對多關(guān)系的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2026-01-01
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),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-11-11

