OkHttpAsyncHttpClientBuilder Class
- java.
lang. Object - com.
azure. core. http. okhttp. OkHttpAsyncHttpClientBuilder
- com.
public class OkHttpAsyncHttpClientBuilder
Builder class responsible for creating instances of HttpClient backed by OkHttp. The client built from this builder can support sending requests synchronously and asynchronously. Use HttpClient#sendSync(HttpRequest, Context) to send the provided request synchronously with contextual information.
Building a new HttpClient instance
HttpClient client = new OkHttpAsyncHttpClientBuilder()
.build();
Building a new HttpClient instance using http proxy.
Configuring the OkHttp client with a proxy is relevant when your application needs to communicate with Azure services through a proxy server.
final String proxyHost = "<proxy-host>"; // e.g. localhost
final int proxyPort = 9999; // Proxy port
ProxyOptions proxyOptions = new ProxyOptions(ProxyOptions.Type.HTTP,
new InetSocketAddress(proxyHost, proxyPort));
HttpClient client = new OkHttpAsyncHttpClientBuilder()
.proxy(proxyOptions)
.build();
Building a new HttpClient instance with connection timeout.
Setting a reasonable connection timeout is particularly important in scenarios where network conditions might be unpredictable or where the server may not be responsive.
final Duration connectionTimeout = Duration.ofSeconds(250); // connection timeout of 250 seconds
HttpClient client = new OkHttpAsyncHttpClientBuilder()
.connectionTimeout(connectionTimeout)
.build();
Building a new HttpClient instance with HTTP/2 Support.
HttpClient client = new OkHttpAsyncHttpClientBuilder()
.build();
It is also possible to create a OkHttp HttpClient that only supports HTTP/2.
// Constructs an HttpClient that only supports HTTP/2.
HttpClient client = new OkHttpAsyncHttpClientBuilder(new OkHttpClient.Builder()
.protocols(Collections.singletonList(Protocol.H2_PRIOR_KNOWLEDGE))
.build())
.build();
Constructor Summary
Constructor | Description |
---|---|
OkHttpAsyncHttpClientBuilder() |
Creates Ok |
OkHttpAsyncHttpClientBuilder(OkHttpClient okHttpClient) |
Creates Ok |
Method Summary
Modifier and Type | Method and Description |
---|---|
Ok |
addNetworkInterceptor(Interceptor networkInterceptor)
Add a network layer interceptor to Http request pipeline. |
Http |
build()
Creates a new Ok |
Ok |
callTimeout(Duration callTimeout)
Sets the default timeout for complete calls. |
Ok |
configuration(Configuration configuration)
Sets the configuration store that is used during construction of the HTTP client. |
Ok |
connectionPool(ConnectionPool connectionPool)
Sets the Http connection pool. |
Ok |
connectionTimeout(Duration connectionTimeout)
Sets the connection timeout for a request to be sent. |
Ok |
dispatcher(Dispatcher dispatcher)
Sets the dispatcher that also composes the thread pool for executing HTTP requests. |
Ok |
followRedirects(boolean followRedirects)
Sets the followRedirect flag on the underlying OkHttp-backed HttpClient. |
Ok |
networkInterceptors(List<Interceptor> networkInterceptors)
Add network layer interceptors to Http request pipeline. |
Ok |
proxy(ProxyOptions proxyOptions)
Sets the proxy. |
Ok |
readTimeout(Duration readTimeout)
Sets the read timeout duration used when reading the server response. |
Ok |
responseTimeout(Duration responseTimeout)
Sets the response timeout duration used when waiting for a server to reply. |
Ok |
writeTimeout(Duration writeTimeout)
Sets the writing timeout for a request to be sent. |
Methods inherited from java.lang.Object
Constructor Details
OkHttpAsyncHttpClientBuilder
public OkHttpAsyncHttpClientBuilder()
Creates OkHttpAsyncHttpClientBuilder.
OkHttpAsyncHttpClientBuilder
public OkHttpAsyncHttpClientBuilder(OkHttpClient okHttpClient)
Creates OkHttpAsyncHttpClientBuilder from the builder of an existing OkHttpClient.
Parameters:
Method Details
addNetworkInterceptor
public OkHttpAsyncHttpClientBuilder addNetworkInterceptor(Interceptor networkInterceptor)
Add a network layer interceptor to Http request pipeline.
Parameters:
Returns:
build
public HttpClient build()
Creates a new OkHttp-backed HttpClient instance on every call, using the configuration set in the builder at the time of the build method call.
Returns:
callTimeout
public OkHttpAsyncHttpClientBuilder callTimeout(Duration callTimeout)
Sets the default timeout for complete calls.
The call timeout spans the entire call: resolving DNS, connecting, writing the request body, server processing, and reading the response body.
Null or Duration#ZERO means no call timeout, otherwise values must be between 1 and Integer#MAX_VALUE when converted to milliseconds.
By default, call timeout is not enabled.
Parameters:
Returns:
configuration
public OkHttpAsyncHttpClientBuilder configuration(Configuration configuration)
Sets the configuration store that is used during construction of the HTTP client.
The default configuration store is a clone of the global configuration store, use NONE to bypass using configuration settings during construction.
Parameters:
Returns:
connectionPool
public OkHttpAsyncHttpClientBuilder connectionPool(ConnectionPool connectionPool)
Sets the Http connection pool.
Parameters:
Returns:
connectionTimeout
public OkHttpAsyncHttpClientBuilder connectionTimeout(Duration connectionTimeout)
Sets the connection timeout for a request to be sent.
The connection timeout begins once the request attempts to connect to the remote host and finishes once the connection is resolved.
If connectTimeout
is null either PROPERTY_AZURE_REQUEST_CONNECT_TIMEOUT or a 10-second timeout will be used, if it is a Duration less than or equal to zero then no timeout will be applied. When applying the timeout the greatest of one millisecond and the value of connectTimeout
will be used.
By default, the connection timeout is 10 seconds.
Parameters:
Returns:
dispatcher
public OkHttpAsyncHttpClientBuilder dispatcher(Dispatcher dispatcher)
Sets the dispatcher that also composes the thread pool for executing HTTP requests.
Parameters:
Returns:
followRedirects
public OkHttpAsyncHttpClientBuilder followRedirects(boolean followRedirects)
Sets the followRedirect flag on the underlying OkHttp-backed HttpClient.
If this is set to 'true' redirects will be followed automatically, and if your HTTP pipeline is configured with a redirect policy it will not be called.
Parameters:
Returns:
networkInterceptors
public OkHttpAsyncHttpClientBuilder networkInterceptors(List
Add network layer interceptors to Http request pipeline.
This replaces all previously-set interceptors.
Parameters:
Returns:
proxy
public OkHttpAsyncHttpClientBuilder proxy(ProxyOptions proxyOptions)
Sets the proxy.
Parameters:
Returns:
readTimeout
public OkHttpAsyncHttpClientBuilder readTimeout(Duration readTimeout)
Sets the read timeout duration used when reading the server response.
The read timeout begins once the first response read is triggered after the server response is received. This timeout triggers periodically but won't fire its operation if another read operation has completed between when the timeout is triggered and completes.
If readTimeout
is null or PROPERTY_AZURE_REQUEST_READ_TIMEOUT or a 60-second timeout will be used, if it is a Duration less than or equal to zero then no timeout period will be applied to response read. When applying the timeout the greatest of one millisecond and the value of readTimeout
will be used.
Parameters:
Returns:
responseTimeout
public OkHttpAsyncHttpClientBuilder responseTimeout(Duration responseTimeout)
Sets the response timeout duration used when waiting for a server to reply.
The response timeout begins once the request write completes and finishes once the first response read is triggered when the server response is received.
If responseTimeout
is null either PROPERTY_AZURE_REQUEST_RESPONSE_TIMEOUT or a 60-second timeout will be used, if it is a Duration less than or equal to zero then no timeout will be applied to the response. When applying the timeout the greatest of one millisecond and the value of responseTimeout
will be used.
Given OkHttp doesn't have an equivalent timeout for just responses, this is handled manually.
Parameters:
Returns:
writeTimeout
public OkHttpAsyncHttpClientBuilder writeTimeout(Duration writeTimeout)
Sets the writing timeout for a request to be sent.
The writing timeout does not apply to the entire request but to the request being sent over the wire. For example a request body which emits 10
8KB
buffers will trigger 10
write operations, the last write tracker will update when each operation completes and the outbound buffer will be periodically checked to determine if it is still draining.
If writeTimeout
is null either PROPERTY_AZURE_REQUEST_WRITE_TIMEOUT or a 60-second timeout will be used, if it is a Duration less than or equal to zero then no write timeout will be applied. When applying the timeout the greatest of one millisecond and the value of writeTimeout
will be used.
Parameters:
Returns:
Applies to
Azure SDK for Java
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for