Hutool encapsulates the tool for sending requests for us. Let’s take a look at what are the commonly used ones!
1. Add dependencies
<dependency> <groupId></groupId> <artifactId>hutool-all</artifactId> <version>5.8.11</version> <!-- Please use the latest version --> </dependency>
2. Send a get request
2.1 Direct URL transmission
import ; import ; public class HttpGetExample { public static void main(String[] args) { // Define the basic URL and path String baseUrl = ""; String path = "/api/test"; // Define parameters String name = "zhangsan"; int age = 21; // Build a complete URL String url = ("{}/{}?name={}&age={}", baseUrl, path, name, age); // Send a GET request String result = (url); // Output response result ("Response: " + result); } }
2.2 Map parameter transfer
import ; import ; import ; public class HttpGetExample { public static void main(String[] args) { // Define the basic URL and path String baseUrl = ""; String path = "/api/test"; // Build a complete URL String url = baseUrl + path; // Define parameters Map<String, Object> params = new HashMap<>(); ("name", "aa"); ("age", 21); // Send a GET request String result = (url, params); // Output response result ("Response: " + result); } }
2.3 Form transfer parameters
import ; import ; import ; import ; public class HttpGetExample { public static void main(String[] args) { // Define the basic URL and path String baseUrl = ""; String path = "/api/test"; // Build a complete URL String url = baseUrl + path; // Define parameters Map<String, Object> params = new HashMap<>(); ("name", "aa"); ("age", 21); // Send a GET request String result = (url) .form(params) .execute() .body(); // Output response result ("Response: " + result); } }
3. Send Post Request
3.1 Json transfer
public static void main(String[] args) { // Define the basic URL and path String baseUrl = ""; String path = "/api/test"; // Build a complete URL String url = baseUrl + path; // Define parameters String jsonString = "{\"token\":\"1234567890\",\"userId\":\"user123\",\"userName\":\"Zhang San\"}"; // Send a GET request String result = (url) .header("Access-Token", token) // If necessary .header("Content-Type","application/json") .body(jsonString) .execute() .body(); // Output response result ("Response: " + result); }
3.2 Form transfer parameters
public static void main(String[] args) { // Define the basic URL and path String baseUrl = ""; String path = "/api/test"; // Build a complete URL String url = baseUrl + path; // Define parameters Map<String, Object> params = new HashMap<>(); ("name", "aa"); ("age", 21); // Send a GET request String result = (url) .header("Content-Type","multipart/form-data;charset=UTF-8") .form(params) .execute() .body(); // Output response result ("Response: " + result); }
This is the article about Spring's HttpRequest sending requests using hutool. This is all about this article. For more related Spring HttpRequest sending requests, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!