Java Spring使用hutool的HttpRequest發(fā)送請(qǐng)求的幾種方式
hutool為我們封裝了發(fā)送請(qǐng)求的工具,我們一起來看看常用的有哪些吧!
1.添加依賴
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.11</version> <!-- 請(qǐng)使用最新版本 -->
</dependency>2.發(fā)送get請(qǐng)求
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 請(qǐng)求
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 請(qǐng)求
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 請(qǐng)求
String result = HttpRequest.get(url)
.form(params)
.execute()
.body();
// 輸出響應(yīng)結(jié)果
System.out.println("Response: " + result);
}
}3. 發(fā)送Post請(qǐng)求
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 請(qǐng)求
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 請(qǐng)求
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ā)送請(qǐng)求的幾種方式的文章就介紹到這了,更多相關(guān)java hutool HttpRequest發(fā)送請(qǐng)求內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- java中的常見幾種發(fā)送http請(qǐng)求實(shí)例
- java中調(diào)用https請(qǐng)求忽略ssl證書認(rèn)證代碼示例
- java?http請(qǐng)求獲取圖片并返回文件流給前端的方法步驟
- java實(shí)現(xiàn)調(diào)用http請(qǐng)求的五種常見方式
- Java中如何模擬HTTP請(qǐng)求并驗(yàn)證功能
- Java如何基于okhttp請(qǐng)求SSE接口流式返回詳解
- Java實(shí)現(xiàn)HttpGet請(qǐng)求傳body參數(shù)
- Java如何使用SSLContext請(qǐng)求https鏈接
- java的http請(qǐng)求工具對(duì)比分析
相關(guān)文章
詳解SpringSecurity中的Authentication信息與登錄流程
這篇文章主要介紹了SpringSecurity中的Authentication信息與登錄流程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
Java遠(yuǎn)程連接Linux服務(wù)器并執(zhí)行命令及上傳文件功能
這篇文章主要介紹了Java遠(yuǎn)程連接Linux服務(wù)器并執(zhí)行命令及上傳文件功能,本文是小編整理的代碼筆記,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-05-05
Java 高并發(fā)十: JDK8對(duì)并發(fā)的新支持詳解
本文主要介紹Java 高并發(fā)JDK8的支持,這里整理了詳細(xì)的資料及1. LongAdder 2. CompletableFuture 3. StampedLock的介紹,有興趣的小伙伴可以參考下2016-09-09
Java 實(shí)戰(zhàn)項(xiàng)目之教材管理系統(tǒng)的實(shí)現(xiàn)流程
讀萬卷書不如行萬里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+SSM+jsp+mysql+maven實(shí)現(xiàn)教材管理系統(tǒng),大家可以在過程中查缺補(bǔ)漏,提升水平2021-11-11

