or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

authentication.mdclient-configuration.mdconnection-management.mdcookie-management.mdhttp-methods.mdindex.mdresponse-handling.md

client-configuration.mddocs/

0

# HTTP Client Configuration

1

2

Apache HttpClient provides flexible configuration options for creating HTTP clients tailored to specific requirements. This includes connection management, timeouts, authentication, SSL settings, and behavioral policies.

3

4

## Client Factory Methods

5

6

### Default Client Creation

7

8

```java { .api }

9

public static CloseableHttpClient createDefault();

10

```

11

12

Creates a default HTTP client with standard configuration including connection pooling, redirect handling, and cookie management.

13

14

```java

15

CloseableHttpClient httpClient = HttpClients.createDefault();

16

```

17

18

### System Client Creation

19

20

```java { .api }

21

public static CloseableHttpClient createSystem();

22

```

23

24

Creates an HTTP client using system properties for proxy settings and authentication.

25

26

```java

27

CloseableHttpClient httpClient = HttpClients.createSystem();

28

```

29

30

### Minimal Client Creation

31

32

```java { .api }

33

public static CloseableHttpClient createMinimal();

34

public static CloseableHttpClient createMinimal(HttpClientConnectionManager connManager);

35

```

36

37

Creates a minimal HTTP client with basic functionality and no connection pooling. The overloaded version allows specifying a custom connection manager.

38

39

```java

40

CloseableHttpClient httpClient = HttpClients.createMinimal();

41

42

// With custom connection manager

43

PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();

44

CloseableHttpClient httpClient = HttpClients.createMinimal(connManager);

45

```

46

47

### Custom Client Builder

48

49

```java { .api }

50

public static HttpClientBuilder custom();

51

```

52

53

Returns a builder for creating customized HTTP clients.

54

55

```java

56

CloseableHttpClient httpClient = HttpClients.custom()

57

.setConnectionManager(connectionManager)

58

.setDefaultRequestConfig(requestConfig)

59

.build();

60

```

61

62

## HttpClientBuilder Configuration

63

64

### Connection Management

65

66

```java { .api }

67

public HttpClientBuilder setConnectionManager(HttpClientConnectionManager connManager);

68

```

69

70

Sets a custom connection manager for controlling connection lifecycle and pooling.

71

72

```java

73

PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();

74

cm.setMaxTotal(100);

75

cm.setDefaultMaxPerRoute(20);

76

77

CloseableHttpClient httpClient = HttpClients.custom()

78

.setConnectionManager(cm)

79

.build();

80

```

81

82

### Request Configuration

83

84

```java { .api }

85

public HttpClientBuilder setDefaultRequestConfig(RequestConfig config);

86

```

87

88

Sets default request configuration for all requests made by this client.

89

90

```java

91

RequestConfig config = RequestConfig.custom()

92

.setSocketTimeout(5000)

93

.setConnectTimeout(5000)

94

.setConnectionRequestTimeout(5000)

95

.build();

96

97

CloseableHttpClient httpClient = HttpClients.custom()

98

.setDefaultRequestConfig(config)

99

.build();

100

```

101

102

### Default Headers

103

104

```java { .api }

105

public HttpClientBuilder setDefaultHeaders(Collection<? extends Header> defaultHeaders);

106

```

107

108

Sets default headers to be included with all requests.

109

110

```java

111

List<Header> defaultHeaders = Arrays.asList(

112

new BasicHeader("User-Agent", "MyApplication/1.0"),

113

new BasicHeader("Accept", "application/json")

114

);

115

116

CloseableHttpClient httpClient = HttpClients.custom()

117

.setDefaultHeaders(defaultHeaders)

118

.build();

119

```

120

121

### Proxy Configuration

122

123

```java { .api }

124

public HttpClientBuilder setProxy(HttpHost proxy);

125

```

126

127

Configures a proxy server for all requests.

128

129

```java

130

HttpHost proxy = new HttpHost("proxy.example.com", 8080, "http");

131

132

CloseableHttpClient httpClient = HttpClients.custom()

133

.setProxy(proxy)

134

.build();

135

```

136

137

### SSL Context

138

139

```java { .api }

140

public HttpClientBuilder setSSLContext(SSLContext sslContext);

141

```

142

143

Sets a custom SSL context for HTTPS connections.

144

145

```java

146

SSLContext sslContext = SSLContextBuilder.create()

147

.loadTrustMaterial(trustStore, trustStorePassword)

148

.build();

149

150

CloseableHttpClient httpClient = HttpClients.custom()

151

.setSSLContext(sslContext)

152

.build();

153

```

154

155

### Authentication Configuration

156

157

```java { .api }

158

public HttpClientBuilder setDefaultCredentialsProvider(CredentialsProvider credentialsProvider);

159

```

160

161

Sets a credentials provider for authentication.

162

163

```java

164

BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();

165

credsProvider.setCredentials(

166

new AuthScope("example.com", 80),

167

new UsernamePasswordCredentials("user", "password")

168

);

169

170

CloseableHttpClient httpClient = HttpClients.custom()

171

.setDefaultCredentialsProvider(credsProvider)

172

.build();

173

```

174

175

### Cookie Store

176

177

```java { .api }

178

public HttpClientBuilder setDefaultCookieStore(CookieStore cookieStore);

179

```

180

181

Sets a custom cookie store for cookie management.

182

183

```java

184

BasicCookieStore cookieStore = new BasicCookieStore();

185

186

CloseableHttpClient httpClient = HttpClients.custom()

187

.setDefaultCookieStore(cookieStore)

188

.build();

189

```

190

191

### Disabling Features

192

193

```java { .api }

194

public HttpClientBuilder disableRedirectHandling();

195

public HttpClientBuilder disableCookieManagement();

196

public HttpClientBuilder disableAuthentication();

197

public HttpClientBuilder disableConnectionState();

198

```

199

200

Disables specific client features for customized behavior.

201

202

```java

203

CloseableHttpClient httpClient = HttpClients.custom()

204

.disableRedirectHandling()

205

.disableCookieManagement()

206

.build();

207

```

208

209

### Redirect and Retry Strategies

210

211

```java { .api }

212

public HttpClientBuilder setRedirectStrategy(RedirectStrategy redirectStrategy);

213

public HttpClientBuilder setRetryHandler(HttpRequestRetryHandler retryHandler);

214

public HttpClientBuilder setServiceUnavailableRetryStrategy(ServiceUnavailableRetryStrategy serviceUnavailRetryStrategy);

215

```

216

217

Sets custom strategies for handling redirects and retries.

218

219

```java

220

// Custom redirect strategy

221

RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();

222

223

// Custom retry handler

224

HttpRequestRetryHandler retryHandler = new DefaultHttpRequestRetryHandler(3, true);

225

226

CloseableHttpClient httpClient = HttpClients.custom()

227

.setRedirectStrategy(redirectStrategy)

228

.setRetryHandler(retryHandler)

229

.build();

230

```

231

232

## Request Configuration

233

234

### RequestConfig Class

235

236

```java { .api }

237

public class RequestConfig;

238

```

239

240

Configuration object for request-specific settings.

241

242

### RequestConfig Methods

243

244

```java { .api }

245

public int getSocketTimeout();

246

public int getConnectTimeout();

247

public int getConnectionRequestTimeout();

248

public boolean isRedirectsEnabled();

249

public boolean isRelativeRedirectsAllowed();

250

public boolean isCircularRedirectsAllowed();

251

public int getMaxRedirects();

252

public HttpHost getProxy();

253

public InetAddress getLocalAddress();

254

public boolean isAuthenticationEnabled();

255

public Collection<String> getTargetPreferredAuthSchemes();

256

public Collection<String> getProxyPreferredAuthSchemes();

257

public boolean isExpectContinueEnabled();

258

public String getCookieSpec();

259

public boolean isContentCompressionEnabled();

260

```

261

262

### RequestConfig Builder

263

264

```java { .api }

265

public static RequestConfig.Builder custom();

266

```

267

268

Creates a builder for request configuration.

269

270

```java { .api }

271

public RequestConfig.Builder setSocketTimeout(int socketTimeout);

272

public RequestConfig.Builder setConnectTimeout(int connectTimeout);

273

public RequestConfig.Builder setConnectionRequestTimeout(int connectionRequestTimeout);

274

public RequestConfig.Builder setRedirectsEnabled(boolean redirectsEnabled);

275

public RequestConfig.Builder setMaxRedirects(int maxRedirects);

276

public RequestConfig.Builder setCircularRedirectsAllowed(boolean circularRedirectsAllowed);

277

public RequestConfig.Builder setRelativeRedirectsAllowed(boolean relativeRedirectsAllowed);

278

public RequestConfig.Builder setProxy(HttpHost proxy);

279

public RequestConfig.Builder setLocalAddress(InetAddress localAddress);

280

public RequestConfig.Builder setAuthenticationEnabled(boolean authenticationEnabled);

281

public RequestConfig.Builder setTargetPreferredAuthSchemes(Collection<String> targetPreferredAuthSchemes);

282

public RequestConfig.Builder setProxyPreferredAuthSchemes(Collection<String> proxyPreferredAuthSchemes);

283

public RequestConfig.Builder setExpectContinueEnabled(boolean expectContinueEnabled);

284

public RequestConfig.Builder setCookieSpec(String cookieSpec);

285

public RequestConfig.Builder setContentCompressionEnabled(boolean contentCompressionEnabled);

286

public RequestConfig build();

287

```

288

289

Example usage:

290

291

```java

292

RequestConfig config = RequestConfig.custom()

293

.setSocketTimeout(10000)

294

.setConnectTimeout(10000)

295

.setConnectionRequestTimeout(5000)

296

.setRedirectsEnabled(true)

297

.setMaxRedirects(5)

298

.build();

299

300

HttpGet httpGet = new HttpGet("https://api.example.com");

301

httpGet.setConfig(config);

302

```

303

304

## Types

305

306

### HttpClientBuilder

307

308

```java { .api }

309

public class HttpClientBuilder;

310

```

311

312

Builder for creating configured HttpClient instances.

313

314

### RequestConfig

315

316

```java { .api }

317

public class RequestConfig {

318

public static class Builder;

319

}

320

```

321

322

Configuration object for HTTP requests with nested Builder class for construction.

323

324

### CloseableHttpClient

325

326

```java { .api }

327

public abstract class CloseableHttpClient implements HttpClient, Closeable;

328

```

329

330

Main HTTP client interface that extends HttpClient and implements Closeable for resource management.