Spring使用hutool的HttpRequest發(fā)送請求的幾種方式
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)于Spring使用hutool的HttpRequest發(fā)送請求的幾種方式的文章就介紹到這了,更多相關(guān)Spring HttpRequest發(fā)送請求內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot項(xiàng)目中添加自定義日志及配置過程
這篇文章主要介紹了springboot項(xiàng)目中添加自定義日志,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-07-07
SpringCloud中使用Sentinel實(shí)現(xiàn)限流的實(shí)戰(zhàn)
限流在很多地方都可以使用的到,本篇博客將介紹如何使用SpringCloud中使用Sentinel實(shí)現(xiàn)限流,從而達(dá)到服務(wù)降級的目的,感興趣的可以了解一下2022-01-01
SpringBoot整合Caffeine實(shí)現(xiàn)本地緩存的實(shí)踐分享
緩存是提升系統(tǒng)性能的一個不可或缺的工具,通過緩存可以避免大部分重復(fù)的請求到數(shù)據(jù)庫層,減少IO鏈接次數(shù),提升整體的響應(yīng)速率,本地緩存中比較常見的比如 Caffeine 緩存,這篇文章將結(jié)合具體的 Springboot 項(xiàng)目搭配 Caffeine 實(shí)現(xiàn)本地緩存的各種使用方式2024-07-07
使用IDEA進(jìn)行安卓開發(fā)的詳細(xì)圖文教程
安卓開發(fā)本身就是Java開發(fā)的一個分支,我們要確保計算機(jī)已經(jīng)安裝好JDK并做好了相關(guān)的配置,下面這篇文章主要給大家介紹了關(guān)于如何使用IDEA進(jìn)行安卓開發(fā)的詳細(xì)圖文教程,需要的朋友可以參考下2023-04-04
分頁技術(shù)原理與實(shí)現(xiàn)之Java+Oracle代碼實(shí)現(xiàn)分頁(二)
這篇文章主要介紹了分頁技術(shù)原理與實(shí)現(xiàn)的第二篇:Java+Oracle代碼實(shí)現(xiàn)分頁,感興趣的小伙伴們可以參考一下2016-06-06

