Spring使用hutool的HttpRequest發(fā)送請求的幾種方式
更新時間:2024年11月22日 09:23:17 作者:Yeast_東
Spring HttpRequest是指Spring框架中的一個對象,它代表了HTTP客戶端發(fā)送給Web服務(wù)器的一次請求,本文給大家介紹了Spring使用hutool的HttpRequest發(fā)送請求的幾種方式,并通過代碼示例講解的非常詳細(xì),需要的朋友可以參考下
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)文章
MyBatis Plus之實(shí)現(xiàn)動態(tài)排序方式
這篇文章主要介紹了MyBatis Plus之實(shí)現(xiàn)動態(tài)排序方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02
基于RecyclerChart的KLine繪制Volume實(shí)現(xiàn)詳解
這篇文章主要為大家介紹了基于RecyclerChart的KLine繪制Volume實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
JavaWeb項目實(shí)現(xiàn)文件上傳動態(tài)顯示進(jìn)度實(shí)例
本篇文章主要介紹了JavaWeb項目實(shí)現(xiàn)文件上傳動態(tài)顯示進(jìn)度實(shí)例,具有一定的參考價值,有興趣的可以了解一下。2017-04-04
Java Stream 的 collect 與 reduce 
在 Java Stream API 中,collect?和?reduce?是兩種強(qiáng)大的終止操作,用于將流中的元素累積為最終結(jié)果,本文將從核心概念、使用場景、性能特性等多個維度進(jìn)行對比分析,感興趣的朋友跟隨小編一起看看吧2025-09-09

