Understand the advanced usage of the HTTP client framework in the Java library
The HTTP client framework in the Java class library provides a simple and powerful way to communicate with other servers on the network.These frameworks allow developers to send requests and obtain response through the HTTP protocol to achieve interaction with remote servers.Benefiting from the advanced usage of these frameworks, developers can flexibly handle various HTTP requests and responses, including custom request head heads, processing abnormal processing, setting agency, and so on.
Below are examples of advanced usage of the HTTP client framework in some Java libraries.
1. Send GET request:
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
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) {
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
HttpGet httpGet = new HttpGet("https://api.example.com/users");
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
String response = EntityUtils.toString(httpEntity);
System.out.println(response);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
2. Send the post request and set the request header and parameters:
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
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) {
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
HttpPost httpPost = new HttpPost("https://api.example.com/users");
httpPost.setHeader("Content-Type", "application/json");
StringEntity requestEntity = new StringEntity(
"{\"username\":\"john\",\"password\":\"secretpassword\"}",
ContentType.APPLICATION_JSON);
httpPost.setEntity(requestEntity);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
String response = EntityUtils.toString(httpEntity);
System.out.println(response);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
3. Processing abnormal and error status code:
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
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) {
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
HttpGet httpGet = new HttpGet("https://api.example.com/users");
HttpResponse httpResponse = httpClient.execute(httpGet);
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode == 200) {
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
String response = EntityUtils.toString(httpEntity);
System.out.println(response);
}
} else {
System.out.println("Error: " + statusCode);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
4. Use agent sending requests:
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
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) {
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
HttpHost proxy = new HttpHost("proxy.example.com", 8080);
RequestConfig config = RequestConfig.custom()
.setProxy(proxy)
.build();
HttpGet httpGet = new HttpGet("https://api.example.com/users");
httpGet.setConfig(config);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
String response = EntityUtils.toString(httpEntity);
System.out.println(response);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
The HTTP client framework in the Java class library provides many advanced usage, enabling developers to communicate with the remote server flexibly.Through learning and application, powerful and reliable network applications can be achieved.