Apache HttpComponents Client is a library of components for building client side HTTP services
npx @tessl/cli install tessl/maven-org-apache-httpcomponents--httpclient@4.5.00
# Apache HttpClient
1
2
Apache HttpClient is a comprehensive Java library for building robust client-side HTTP services. It provides essential functionality for HTTP/1.1 and HTTP/2 protocols, connection pooling, authentication mechanisms, cookie management, SSL/TLS support, proxy configuration, and automatic redirect handling.
3
4
## Package Information
5
6
- **Package Name**: org.apache.httpcomponents:httpclient
7
- **Package Type**: maven
8
- **Language**: Java
9
- **Installation**: Add to Maven POM:
10
```xml
11
<dependency>
12
<groupId>org.apache.httpcomponents</groupId>
13
<artifactId>httpclient</artifactId>
14
<version>4.5.14</version>
15
</dependency>
16
```
17
18
## Core Imports
19
20
```java
21
import org.apache.http.client.HttpClient;
22
import org.apache.http.impl.client.HttpClients;
23
import org.apache.http.client.methods.HttpGet;
24
import org.apache.http.HttpResponse;
25
```
26
27
For closeable HTTP clients (recommended for proper resource management):
28
29
```java
30
import org.apache.http.impl.client.CloseableHttpClient;
31
import org.apache.http.impl.client.HttpClients;
32
import org.apache.http.client.methods.CloseableHttpResponse;
33
```
34
35
## Basic Usage
36
37
```java
38
import org.apache.http.impl.client.CloseableHttpClient;
39
import org.apache.http.impl.client.HttpClients;
40
import org.apache.http.client.methods.HttpGet;
41
import org.apache.http.client.methods.CloseableHttpResponse;
42
import org.apache.http.util.EntityUtils;
43
44
// Create HTTP client
45
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
46
// Create GET request
47
HttpGet httpGet = new HttpGet("https://api.example.com/data");
48
49
// Execute request
50
try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
51
// Check response status
52
int statusCode = response.getStatusLine().getStatusCode();
53
54
// Get response content
55
String responseBody = EntityUtils.toString(response.getEntity());
56
System.out.println("Status: " + statusCode);
57
System.out.println("Response: " + responseBody);
58
}
59
} catch (Exception e) {
60
e.printStackTrace();
61
}
62
```
63
64
## Architecture
65
66
Apache HttpClient is built around several key architectural patterns:
67
68
- **HttpClient Interface**: Main interface for HTTP request execution with implementations for different scenarios
69
- **Connection Management**: Pooled and basic connection managers for efficient resource utilization
70
- **Request/Response Model**: Typed HTTP method classes (GET, POST, PUT, etc.) with comprehensive header and entity support
71
- **Configuration System**: Builder patterns for client configuration with request-specific overrides
72
- **Authentication Framework**: Pluggable authentication schemes (Basic, Digest, NTLM, Kerberos) with credential management
73
- **Protocol Handlers**: Support for redirects, retries, and proxy handling with customizable strategies
74
75
This layered architecture provides both simple default behavior and extensive customization capabilities, making it suitable for everything from basic HTTP requests to complex enterprise integration scenarios.
76
77
## Capabilities
78
79
### HTTP Client Creation and Configuration
80
81
Core functionality for creating and configuring HTTP client instances with connection management, timeouts, authentication, and SSL settings.
82
83
```java { .api }
84
public static CloseableHttpClient createDefault();
85
public static CloseableHttpClient createSystem();
86
public static CloseableHttpClient createMinimal();
87
public static HttpClientBuilder custom();
88
```
89
90
[HTTP Client Configuration](./client-configuration.md)
91
92
### HTTP Request Methods
93
94
Implementation classes for all standard HTTP methods with support for headers, entities, and request-specific configuration.
95
96
```java { .api }
97
public class HttpGet extends HttpRequestBase;
98
public class HttpPost extends HttpEntityEnclosingRequestBase;
99
public class HttpPut extends HttpEntityEnclosingRequestBase;
100
public class HttpDelete extends HttpRequestBase;
101
```
102
103
[HTTP Request Methods](./http-methods.md)
104
105
### Connection Management
106
107
Connection pooling and lifecycle management for efficient resource utilization and performance optimization.
108
109
```java { .api }
110
public interface HttpClientConnectionManager;
111
public class PoolingHttpClientConnectionManager implements HttpClientConnectionManager;
112
public class BasicHttpClientConnectionManager implements HttpClientConnectionManager;
113
```
114
115
[Connection Management](./connection-management.md)
116
117
### Authentication and Security
118
119
Comprehensive authentication mechanisms including Basic, Digest, NTLM, and Kerberos with credential management and SSL/TLS support.
120
121
```java { .api }
122
public interface CredentialsProvider;
123
public class UsernamePasswordCredentials implements Credentials;
124
public class NTCredentials implements Credentials;
125
public class AuthScope;
126
```
127
128
[Authentication and Security](./authentication.md)
129
130
### Cookie Management
131
132
Full HTTP cookie handling with storage, expiration, and policy management for session-based applications.
133
134
```java { .api }
135
public interface CookieStore;
136
public interface Cookie;
137
public class BasicCookieStore implements CookieStore;
138
```
139
140
[Cookie Management](./cookie-management.md)
141
142
### Response Handling
143
144
Flexible response processing with handler interfaces, closeable responses for resource management, and utility methods for content extraction.
145
146
```java { .api }
147
public interface ResponseHandler<T>;
148
public interface CloseableHttpResponse extends HttpResponse, Closeable;
149
```
150
151
[Response Handling](./response-handling.md)
152
153
### Utility Classes
154
155
Helper classes for URI building, URL encoding, and resource cleanup operations.
156
157
```java { .api }
158
public class URIBuilder;
159
public class URLEncodedUtils;
160
public class HttpClientUtils;
161
```
162
163
**URIBuilder** - Builder for constructing URIs with query parameters
164
**URLEncodedUtils** - Utility methods for URL encoding and form data
165
**HttpClientUtils** - Utility methods for safely closing HTTP resources