Java Spring使用hutool的HttpRequest發(fā)送請求的幾種方式
更新時間:2024年11月22日 10:33:56 作者:Yeast_東
文章介紹了Hutool庫中用于發(fā)送HTTP請求的工具,包括添加依賴、發(fā)送GET和POST請求的方法,以及GET請求的不同參數(shù)傳遞方式,感興趣的朋友跟隨小編一起看看吧
hutool為我們封裝了發(fā)送請求的工具,我們一起來看看常用的有哪些吧!
1.添加依賴
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.11</version> <!-- 請使用最新版本 -->
</dependency>2.發(fā)送get請求
2.1 直接url傳參
import cn.hutool.http.HttpUtil;
import cn.hutool.core.util.StrUtil;
public class HttpGetExample {
public static void main(String[] args) {
// 定義基礎(chǔ) URL 和路徑
String baseUrl = "http://example.com";
String path = "/api/test";
// 定義參數(shù)
String name = "zhangsan";
int age = 21;
// 構(gòu)建完整的 URL
String url = StrUtil.format("{}/{}?name={}&age={}", baseUrl, path, name, age);
// 發(fā)送 GET 請求
String result = HttpUtil.get(url);
// 輸出響應(yīng)結(jié)果
System.out.println("Response: " + result);
}
}2.2 Map傳參
import cn.hutool.http.HttpUtil;
import java.util.HashMap;
import java.util.Map;
public class HttpGetExample {
public static void main(String[] args) {
// 定義基礎(chǔ) URL 和路徑
String baseUrl = "http://example.com";
String path = "/api/test";
// 構(gòu)建完整的 URL
String url = baseUrl + path;
// 定義參數(shù)
Map<String, Object> params = new HashMap<>();
params.put("name", "aa");
params.put("age", 21);
// 發(fā)送 GET 請求
String result = HttpUtil.get(url, params);
// 輸出響應(yīng)結(jié)果
System.out.println("Response: " + result);
}
}2.3 Form傳參
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import java.util.HashMap;
import java.util.Map;
public class HttpGetExample {
public static void main(String[] args) {
// 定義基礎(chǔ) URL 和路徑
String baseUrl = "http://example.com";
String path = "/api/test";
// 構(gòu)建完整的 URL
String url = baseUrl + path;
// 定義參數(shù)
Map<String, Object> params = new HashMap<>();
params.put("name", "aa");
params.put("age", 21);
// 發(fā)送 GET 請求
String result = HttpRequest.get(url)
.form(params)
.execute()
.body();
// 輸出響應(yīng)結(jié)果
System.out.println("Response: " + result);
}
}3. 發(fā)送Post請求
3.1 Json傳參
public static void main(String[] args) {
// 定義基礎(chǔ) URL 和路徑
String baseUrl = "http://example.com";
String path = "/api/test";
// 構(gòu)建完整的 URL
String url = baseUrl + path;
// 定義參數(shù)
String jsonString = "{\"token\":\"1234567890\",\"userId\":\"user123\",\"userName\":\"張三\"}";
// 發(fā)送 GET 請求
String result = HttpRequest.post(url)
.header("Access-Token", token) // 如果需要
.header("Content-Type","application/json")
.body(jsonString)
.execute()
.body();
// 輸出響應(yīng)結(jié)果
System.out.println("Response: " + result);
}3.2 Form傳參
public static void main(String[] args) {
// 定義基礎(chǔ) URL 和路徑
String baseUrl = "http://example.com";
String path = "/api/test";
// 構(gòu)建完整的 URL
String url = baseUrl + path;
// 定義參數(shù)
Map<String, Object> params = new HashMap<>();
params.put("name", "aa");
params.put("age", 21);
// 發(fā)送 GET 請求
String result = HttpRequest.post(url)
.header("Content-Type","multipart/form-data;charset=UTF-8")
.form(params)
.execute()
.body();
// 輸出響應(yīng)結(jié)果
System.out.println("Response: " + result);
}到此這篇關(guān)于Java Spring使用hutool的HttpRequest發(fā)送請求的幾種方式的文章就介紹到這了,更多相關(guān)java hutool HttpRequest發(fā)送請求內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解SpringSecurity中的Authentication信息與登錄流程
這篇文章主要介紹了SpringSecurity中的Authentication信息與登錄流程,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09
Java遠(yuǎn)程連接Linux服務(wù)器并執(zhí)行命令及上傳文件功能
這篇文章主要介紹了Java遠(yuǎn)程連接Linux服務(wù)器并執(zhí)行命令及上傳文件功能,本文是小編整理的代碼筆記,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-05-05
Java 高并發(fā)十: JDK8對并發(fā)的新支持詳解
本文主要介紹Java 高并發(fā)JDK8的支持,這里整理了詳細(xì)的資料及1. LongAdder 2. CompletableFuture 3. StampedLock的介紹,有興趣的小伙伴可以參考下2016-09-09
Java 實戰(zhàn)項目之教材管理系統(tǒng)的實現(xiàn)流程
讀萬卷書不如行萬里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+SSM+jsp+mysql+maven實現(xiàn)教材管理系統(tǒng),大家可以在過程中查缺補漏,提升水平2021-11-11

