In -depth understanding of the HTTPZ native client framework in the Java library
In -depth understanding of the HTTP native client framework in the Java class library
Overview:
HTTP (Hypertext Transfer Protocol) is a protocol for transmitting super text, which is the basis for building the Internet.In the Java library, developers can use the HTTP native client framework to interact with other computer systems to send and receive HTTP requests.This article will explore the HTTP native client framework in the Java class library and provide some Java code examples.
1. HTTP native client framework in Java:
Java provides multiple HTTP native client frameworks, the most commonly used are UrlConnection and HTTPClient.The characteristics and usage methods will be introduced below.
1.1 URLConnection:
UrlConnection is a built -in HTTP client framework in Java, which provides a set of classes and interfaces to create and manage connections between the server.Using UrlConnection can easily send Get and Post requests and process the server's response.
Example code 1: Send GET request
URL url = new URL("http://example.com/api");
URLConnection connection = url.openConnection();
connection.setRequestMethod("GET");
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
Example code 2: Send post request
URL url = new URL("http://example.com/api");
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
writer.write("data=example");
writer.flush();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
writer.close();
reader.close();
1.2 HttpClient:
HTTPClient is a powerful open source HTTP client library developed by the Apache Foundation. It provides richer features and APIs that are easier to use.The version of the HTTPClient in the Java library is HTTPClient 4.x, which is one of the most popular HTTP client frameworks in the Java class library.
Example code 3: Send GET request
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet request = new HttpGet("http://example.com/api");
CloseableHttpResponse response = httpClient.execute(request);
try {
HttpEntity entity = response.getEntity();
BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
EntityUtils.consume(entity);
} finally {
response.close();
}
Example code 4: Send post request
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost request = new HttpPost("http://example.com/api");
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("data", "example"));
request.setEntity(new UrlEncodedFormEntity(params));
CloseableHttpResponse response = httpClient.execute(request);
try {
HttpEntity entity = response.getEntity();
BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
EntityUtils.consume(entity);
} finally {
response.close();
}
2. Summary:
This article introduces the HTTP native client framework in the Java class library, including UrlConnection and HTTPClient.UrlConnection is a built -in HTTP client framework of Java, which provides basic functions and simple use; HTTPClient is an HTTP client library developed by the Apache Foundation. It has more powerful functions and easier to use APIs.Developers can choose a suitable framework according to actual needs for HTTP requests.The example code provided above can help developers understand how to use these two frameworks to send GET and Post requests.