Use Apache httpcore to implement HTTP through

Use Apache httpcore to implement HTTP communication HTTPCORE is one of the open source projects of Apache, which provides core components to implement HTTP communication.In Java, we can use HTTPCORE to send HTTP requests and process HTTP responses.Here are some examples of examples that use HTTPCORE to implement HTTP communication. 1. Send GET request import java.io.IOException; 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) { HttpClient httpClient = HttpClientBuilder.create().build(); HttpGet httpGet = new HttpGet("http://example.com"); try { HttpResponse response = httpClient.execute(httpGet); String responseBody = EntityUtils.toString(response.getEntity()); System.out.println(responseBody); } catch (IOException e) { e.printStackTrace(); } } } The above code demonstrates how to use HTTPCORE to send a simple GET request and print out the response body. 2. Send post request import java.io.IOException; 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) { HttpClient httpClient = HttpClientBuilder.create().build(); HttpPost httpPost = new HttpPost("http://example.com"); httpPost.addHeader("Content-Type", "application/json"); String requestBody = "{\"key\":\"value\"}"; try { httpPost.setEntity(new StringEntity(requestBody)); HttpResponse response = httpClient.execute(httpPost); String responseBody = EntityUtils.toString(response.getEntity()); System.out.println(responseBody); } catch (IOException e) { e.printStackTrace(); } } } The above code demonstrates how to use HTTPCORE to send a simple post request, and at the same time, it also demonstrates how to set the request body and request header. 3. Process cookies import java.io.IOException; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.cookie.Cookie; import org.apache.http.impl.client.BasicCookieStore; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.cookie.BasicClientCookie; import org.apache.http.util.EntityUtils; public class HttpClientExample { public static void main(String[] args) { BasicCookieStore cookieStore = new BasicCookieStore(); BasicClientCookie cookie = new BasicClientCookie("name", "value"); cookie.setDomain("example.com"); cookie.setPath("/"); cookieStore.addCookie(cookie); HttpClient httpClient = HttpClientBuilder.create().setDefaultCookieStore(cookieStore).build(); HttpGet httpGet = new HttpGet("http://example.com"); try { HttpResponse response = httpClient.execute(httpGet); String responseBody = EntityUtils.toString(response.getEntity()); System.out.println(responseBody); for (Cookie c : cookieStore.getCookies()) { System.out.println(c.getName() + ": " + c.getValue()); } } catch (IOException e) { e.printStackTrace(); } } } The above code demonstrates how to use HTTPCORE to send a GET request with cookies and print a response body and return cookie. These example code only shows how to use Apache HTTPCORE to achieve the basic function of HTTP communication.According to specific needs, you can also use HTTPCORE to process HTTPS communication, process redirection, set up request timeout, etc.