How to use the Request framework to send HTTP requests in the Java class library
How to use the Request framework to send HTTP requests in the Java class library
In Java development, we often need to communicate with the external HTTP interface.To simplify this process, we can use some mature Java class libraries to send HTTP requests.Among them, the Request framework is a very common and powerful tool. It provides simple APIs to help us quickly build and send HTTP requests.This article will introduce how to send HTTP requests with the Request framework in the Java library and provide some Java code examples.
Preparation:
Before starting, we need to ensure that your Java project has introduced the Request framework dependence.
Initiating GET request:
First of all, let's look at the simplest example. How to use the Request framework to initiate a GET request:
import kong.unirest.HttpResponse;
import kong.unirest.Request;
import kong.unirest.Unirest;
public class HttpExample {
public static void main(String[] args) {
HttpResponse<String> response = Unirest.get("http://example.com/api/end-point")
.asString();
System.out.println("Response code: " + response.getStatus());
System.out.println("Response body: " + response.getBody());
}
}
In this example, we use the GET () method of the Unirest class to create a GET request instance and specify the URL of the request.We then call the ASSTRING () method to send a request and get a response.Finally, we printed the response status code and content.
Launch the post request:
In addition to sending GET requests, we often need to send POST requests to submit data to the server.Below is an example of sending post requests using the Request framework:
import kong.unirest.HttpResponse;
import kong.unirest.JsonNode;
import kong.unirest.Unirest;
public class HttpExample {
public static void main(String[] args) {
HttpResponse<JsonNode> response = Unirest.post("http://example.com/api/end-point")
.header("Content-Type", "application/json")
.body("{\"name\":\"John\", \"age\":30}")
.asJson();
System.out.println("Response code: " + response.getStatus());
System.out.println("Response body: " + response.getBody());
}
}
In this example, we use the post () method of the Unirest class to create an POST request instance and specify the URL of the request.We also use the Header () method to set the Content-Type in the request head to set the content of the request body for Application/JSON, and the body () method.Finally, we call the asjson () method to send a request and get a response.Similarly, we have printed the status code and content of the response.
The use of the Request framework to send the HTTP request is very simple and flexible. We can set information such as the requested URL, request head, request body and other information as needed, and can easily obtain the response status code and content.By using the Request framework, we can communicate with the external HTTP interface efficiently to achieve various functional needs.