Core :: http client framework in the java class library
Use the Core :: HTTP client framework in the java class library
In Java applications, we often need to communicate with external API or web services.Core :: HTTP client framework is a powerful and flexible tool in the Java class library to simplify the process of communicating with the HTTP server.This guide will demonstrate how to use Core :: HTTP client framework in Java to send HTTP requests and deal with response.
Install Core :: http client framework
To use Core :: HTTP client framework, you need to add corresponding dependencies to your Java project.You can add the following dependencies in the configuration file of the project construction tool (such as Maven or Gradle):
<!-- Maven -->
<dependency>
<groupId>org.apache.hc</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
<!-- Gradle -->
implementation 'org.apache.hc:httpclient:4.5.13'
Send a GET request
The following is an example of using Core :: http client framework to send GET requests:
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
public class HttpClientExample {
public static void main(String[] args) throws Exception {
// Create HTTPCLIENT instance
HttpClient httpClient = HttpClientBuilder.create().build();
// Create an HTTPGET request and specify URL
HttpGet httpGet = new HttpGet("https://api.example.com/users");
// Send a request and get a response
HttpResponse response = httpClient.execute(httpGet);
// Get the status code of the response
int statusCode = response.getStatusLine().getStatusCode();
System.out.println("Status Code: " + statusCode);
// Get the response content
String responseBody = EntityUtils.toString(response.getEntity());
System.out.println("Response Body: " + responseBody);
// Turn off the httpclient connection
httpClient.close();
}
}
In the above example, we first created an HTTPClient instance, then created an HTTPGET request object, and specified the URL to be sent.We use the `Execute` method of httpclient to send a request and get a response.Then, we can obtain the response status code through the `GetStatusLine` method, and obtain the response content through the` EntityUtills.tostring` method.Finally, we closed the connection of the httpclient.
Send a post request
If you need to send a POST request, you can use the HTTPPOST object.The following is an example of using Core :: HTTP client framework to send post request:
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
public class HttpClientExample {
public static void main(String[] args) throws Exception {
// Create HTTPCLIENT instance
HttpClient httpClient = HttpClientBuilder.create().build();
// Create HTTPPOST request and specify URL
HttpPost httpPost = new HttpPost("https://api.example.com/users");
// Set the request body
String requestBody = "{\"name\": \"John\", \"age\": 30}";
StringEntity entity = new StringEntity(requestBody);
httpPost.setEntity(entity);
// Send a request and get a response
HttpResponse response = httpClient.execute(httpPost);
// Get the status code of the response
int statusCode = response.getStatusLine().getStatusCode();
System.out.println("Status Code: " + statusCode);
// Get the response content
String responseBody = EntityUtils.toString(response.getEntity());
System.out.println("Response Body: " + responseBody);
// Turn off the httpclient connection
httpClient.close();
}
}
In the above example, we created an HTTPPOST request object and set the request body using the `setentity` method.We then send a request and obtain a response, and the status code and content of the response.
Summarize
Core :: HTTP client framework provides a powerful tool for sending HTTP requests and processing responses in Java.This guide demonstrates how to use the framework to send Get and Post requests and deal with response.According to actual needs, you can further explore more functions and configuration options of the framework.