Technical principles and practice of the Commons HTTP Client framework

Technical principles and practice of the Commons HTTP Client framework Overview Commons HTTP Client is a powerful and flexible HTTP client library that provides a simple and easy -to -use way to communicate with the web server.This article will introduce the technical principles and practical methods of the Commons HTTP Client framework, and provide some Java code examples to help readers better understand and apply the framework. Technical principle Commons HTTP Client is a package implemented based on the Java HTTP protocol, which can be used to send HTTP requests and receive responses.It provides a set of APIs for processing HTTP requests, including creating requests, setting request parameters, sending requests, processing responses, and so on. 1. Create HTTPCLIENT instance To use the Commons http client, we must first create an HTTPClient instance.HTTPClient is the core class of the entire framework, which is responsible for managing HTTP connection, certification, timeout settings, etc. CloseableHttpClient httpClient = HttpClients.createDefault(); 2. Create HTTPGET or HTTPPOST request To send a GET request or post request, you can use the HTTPGET or HTTPPOST class to create a request object. HttpGet httpGet = new HttpGet("http://www.example.com"); HttpPost httpPost = new HttpPost("http://www.example.com"); 3. Set request parameters You can set the request parameter by adding the request head, request body, query parameter, etc. httpGet.addHeader("Content-Type", "application/json"); httpPost.setEntity(new StringEntity(jsonData)); // Set the query parameter URIBuilder uriBuilder = new URIBuilder("http://www.example.com"); uriBuilder.addParameter("param1", "value1"); uriBuilder.addParameter("param2", "value2"); httpGet.setURI(uriBuilder.build()); 4. Send a request and deal with response Use the EXECUTE method of HTTPClient to send a request and use the HTTPRESPONSE object to process the response. HttpResponse response = httpClient.execute(httpGet); You can obtain information from the response status code, response head, response body and other information from the HTTPRESPONSE object. int statusCode = response.getStatusLine().getStatusCode(); Header[] headers = response.getAllHeaders(); String responseBody = EntityUtils.toString(response.getEntity()); 5. Release resources When you no longer use the HTTPCLIENT instance, you need to explicitly release resources and close the connection. httpClient.close(); Practice method Below we will use a simple example to demonstrate how to use the Commons HTTP Client to send GET and Post requests. import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; public class HttpClientExample { public static void main(String[] args) { CloseableHttpClient httpClient = HttpClients.createDefault(); // Send GET request try { HttpGet httpGet = new HttpGet("http://www.example.com"); HttpResponse response = httpClient.execute(httpGet); int statusCode = response.getStatusLine().getStatusCode(); String responseBody = EntityUtils.toString(response.getEntity()); System.out.println ("Get request status code:" + StatusCode); System.out.println ("Get request response:" + Responsebody); } catch (Exception e) { e.printStackTrace(); } // Send post request try { HttpPost httpPost = new HttpPost("http://www.example.com"); httpPost.addHeader("Content-Type", "application/json"); String jsonBody = "{\"name\":\"John\", \"age\":30}"; httpPost.setEntity(new StringEntity(jsonBody)); HttpResponse response = httpClient.execute(httpPost); int statusCode = response.getStatusLine().getStatusCode(); String responseBody = EntityUtils.toString(response.getEntity()); System.out.println ("Post request status code:" + StatusCode); System.out.println ("Post request response:" + responsebody); } catch (Exception e) { e.printStackTrace(); } try { httpClient.close(); } catch (Exception e) { e.printStackTrace(); } } } Through the above examples, we can find that the use of the Commons HTTP Client framework is very simple and flexible.It provides various methods to handle different types of HTTP requests and has good performance and stability. in conclusion This article introduces the technical principles and practical methods of the Commons HTTP Client framework, and provides related Java code examples.It is hoped that readers can have a deeper understanding of the Commons HTTP Client framework by reading this article, and flexibly use the framework in actual projects.