Top Description Fields Constructors Methods
org.eclipse.jetty.client

public Class HttpClient

extends ContainerLifeCycle
Class Inheritance
Annotations
@ManagedObject:The HTTP client
Imports
java.net.InetSocketAddress, .Socket, .SocketAddress, .URI, java.nio.channels.SelectionKey, .SocketChannel, java.time.Duration, java.util.ArrayList, .List, .Locale, .Map, .Objects, java.util.concurrent.ConcurrentHashMap, .ConcurrentMap, .ExecutionException, .Executor, .TimeoutException, org.eclipse.jetty.client.internal.HttpAuthenticationStore, .NotifyingRequestListeners, org.eclipse.jetty.client.transport.HttpClientTransportOverHTTP, .HttpConversation, .HttpDestination, .HttpRequest, org.eclipse.jetty.http.HttpCompliance, .HttpCookie, .HttpCookieStore, .HttpField, .HttpHeader, .HttpMethod, .HttpParser, .HttpScheme, .SetCookieParser, org.eclipse.jetty.io.ArrayByteBufferPool, .ByteBufferPool, .ClientConnectionFactory, .ClientConnector, org.eclipse.jetty.io.ssl.SslClientConnectionFactory, org.eclipse.jetty.util.Fields, .Jetty, .ProcessorUtils, .Promise, .SocketAddressResolver, org.eclipse.jetty.util.annotation.ManagedAttribute, .ManagedObject, org.eclipse.jetty.util.component.ContainerLifeCycle, org.eclipse.jetty.util.ssl.SslContextFactory, org.eclipse.jetty.util.thread.QueuedThreadPool, .ScheduledExecutorScheduler, .Scheduler, .Sweeper, .ThreadPool, org.slf4j.Logger, .LoggerFactory

HttpClient provides an efficient, asynchronous, non-blocking implementation to perform HTTP requests to a server through a simple API that offers also blocking semantic.

HttpClient provides easy-to-use methods such as GET(String) that allow to perform HTTP requests in a one-liner, but also gives the ability to fine tune the configuration of requests via HttpClient#newRequest(URI).

HttpClient acts as a central configuration point for network parameters (such as idle timeouts) and HTTP parameters (such as whether to follow redirects).

HttpClient transparently pools connections to servers, but allows direct control of connections for cases where this is needed.

HttpClient also acts as a central configuration point for cookies, via getHttpCookieStore().

Typical usage:

HttpClient httpClient = new HttpClient();
httpClient.start();

// One liner:
httpClient.GET("http://localhost:8080/").getStatus();

// Building a request with a timeout
ContentResponse response = httpClient.newRequest("http://localhost:8080")
        .timeout(5, TimeUnit.SECONDS)
        .send();
int status = response.status();

// Asynchronously
httpClient.newRequest("http://localhost:8080").send(new Response.CompleteListener()
{
    @Override
    public void onComplete(Result result)
    {
        ...
    }
});

Field Summary

Modifier and TypeField and Description
private long
private HttpField
private AuthenticationStore
private final ClientConnector
private HttpCookieStore
private final ContentDecoder.Factories
private String
private long
private final ConcurrentMap<Origin, HttpDestination>
private Sweeper
private boolean
private final ProtocolHandlers
private HttpCompliance
private static final Logger
private int
private int
private int
private int
private String
private final ProxyConfiguration
private int
private final RequestListeners
private SocketAddressResolver
private int
private boolean
private final HttpClientTransport
private boolean
private boolean
public static final String

Constructor Summary

AccessConstructor and Description
public
HttpClient()

Creates a HttpClient instance that can perform HTTP/1.1 requests to non-TLS and TLS destinations.

public

Method Summary

Modifier and TypeMethod and Description
protected Request
copyRequest(Request oldRequest, URI newURI)

public Origin
createOrigin(Request request, Origin.Protocol protocol)

protected void
doStart()

Overrides org.eclipse.jetty.util.component.ContainerLifeCycle.doStart.

Starts the managed lifecycle beans in the order they were added.
protected void
doStop()

Overrides org.eclipse.jetty.util.component.ContainerLifeCycle.doStop.

Stops the managed lifecycle beans in the reverse order they were added.
public ProtocolHandler
public ContentResponse

Returns:

the ContentResponse for the request
FORM
(String
the URI to POST
uri
,
Fields
the fields composing the form name/value pairs
fields
)

Performs a POST request to the specified URI with the given form parameters.

public ContentResponse

Returns:

the ContentResponse for the request
FORM
(URI
the URI to POST
uri
,
Fields
the fields composing the form name/value pairs
fields
)

Performs a POST request to the specified URI with the given form parameters.

public ContentResponse

Returns:

the ContentResponse for the request
GET
(String
the URI to GET
uri
)

Performs a GET request to the specified URI.

public ContentResponse

Returns:

the ContentResponse for the request
GET
(URI
the URI to GET
uri
)

Performs a GET request to the specified URI.

public long

Returns:

the timeout, in milliseconds, for the default SocketAddressResolver created at startup
getAddressResolutionTimeout
()

public AuthenticationStore

Returns:

the authentication store associated with this instance
getAuthenticationStore
()

Get the authentication store associated with this instance.

public SocketAddress

Returns:

the address to bind socket channels to
getBindAddress
()

public ByteBufferPool

Returns:

the ByteBufferPool of this HttpClient
getByteBufferPool
()

Get the ByteBufferPool of this HttpClient.

public long

Returns:

the max time, in milliseconds, a connection can take to connect to destinations. Zero value means infinite timeout.
getConnectTimeout
()

public ContentDecoder.Factories

Returns:

a set of ContentDecoder.Factory that can be used to add and remove content decoder factories
getContentDecoderFactories
()

Returns a non thread-safe set of ContentDecoder.Factorys that can be modified before performing requests.

public String

Returns:

the default content type for request content
getDefaultRequestContentType
()

public long

Returns:

the time in ms after which idle destinations are removed
getDestinationIdleTimeout
()

The default value is 0

public List<Destination>

Returns:

the list of destinations known to this HttpClient.
getDestinations
()

public Executor

Returns:

the Executor of this HttpClient
getExecutor
()

Get the Executor of this HttpClient.

public HttpCompliance

Returns:

the HttpCompliance instance
getHttpCompliance
()

Gets the http compliance mode for parsing http responses.

public HttpCookieStore

Returns:

the cookie store associated with this instance
getHttpCookieStore
()

Get the cookie store associated with this instance.

public long

Returns:

the max time, in milliseconds, a connection can be idle (that is, without traffic of bytes in either direction)
getIdleTimeout
()

public int

Returns:

the max number of connections that this HttpClient opens to Destinations
getMaxConnectionsPerDestination
()

public int

Returns:

the max number of HTTP redirects that are followed in a conversation
getMaxRedirects
()

public int

Returns:

the max number of requests that may be queued to a Destination.
getMaxRequestsQueuedPerDestination
()

public int

Returns:

the max size in bytes of the response headers
getMaxResponseHeadersSize
()

public String

Returns:

the name of this HttpClient
getName
()

public ProtocolHandlers
public ProxyConfiguration

Returns:

the forward proxy configuration
getProxyConfiguration
()

Get the forward proxy configuration.

public int

Returns:

the size of the buffer (in bytes) used to write requests
getRequestBufferSize
()

public RequestListeners

Returns:

a RequestListeners instance that can be used to add request listeners
getRequestListeners
()

Returns a non thread-safe container of Request.Listeners that allows to add request listeners before performing requests.

public int

Returns:

the size of the buffer (in bytes) used to read responses
getResponseBufferSize
()

public Scheduler

Returns:

the Scheduler of this HttpClient
getScheduler
()

Get the Scheduler of this HttpClient.

public SocketAddressResolver

Returns:

the SocketAddressResolver of this HttpClient
getSocketAddressResolver
()

Get the SocketAddressResolver of this HttpClient.

public SslContextFactory.Client

Returns:

the SslContextFactory.Client that manages TLS encryption
getSslContextFactory
()

public HttpClientTransport
public HttpField

Returns:

the "User-Agent" HTTP field of this HttpClient
getUserAgentField
()

Get the "User-Agent" HTTP field of this HttpClient.

public boolean

Returns:

whether connect() operations are performed in blocking mode
isConnectBlocking
()

public boolean

Returns:

whether this HttpClient follows HTTP redirects
isFollowRedirects
()

public boolean

Returns:

whether request events must be strictly ordered
isStrictEventOrdering
()

public boolean

Returns:

whether to use direct ByteBuffers for reading
isUseInputDirectByteBuffers
()

public boolean

Returns:

whether to use direct ByteBuffers for writing
isUseOutputDirectByteBuffers
()

public void
private HttpConversation
private HttpRequest
newHttpRequest(HttpConversation conversation, URI uri)

public Request

Returns:

the request just created
newRequest
(String
the request host
host
,
int
the request port
port
)

Creates a new request with the "http" scheme and the specified host and port

public Request

Returns:

the request just created
newRequest
(String
the request absolute URI
uri
)

Creates a new request with the specified absolute URI in string format.

public Request

Returns:

the request just created
newRequest
(URI
the request absolute URI
uri
)

Creates a new request with the specified absolute URI.

public ClientConnectionFactory
public static int
normalizePort(String scheme, int port)

public Request

Returns:

the POST request
POST
(String
the URI to POST to
uri
)

Creates a POST request to the specified URI.

public Request

Returns:

the POST request
POST
(URI
the URI to POST to
uri
)

Creates a POST request to the specified URI.

public void
putCookie(URI uri, HttpField field)

public boolean
public Destination
public Destination

Returns:

the destination for the given origin
resolveDestination
(Origin
the origin that identifies the destination
origin
)

Returns, creating it if absent, the destination with the given origin.

public void
setAddressResolutionTimeout(long
the timeout, in milliseconds, for the default SocketAddressResolver created at startup
addressResolutionTimeout
)

Sets the socket address resolution timeout used by the default SocketAddressResolver created by this HttpClient at startup.

public void
setAuthenticationStore(AuthenticationStore
the authentication store associated with this instance
authenticationStore
)

Set the authentication store associated with this instance.

public void
setBindAddress(SocketAddress
the address to bind socket channels to
bindAddress
)

public void
setByteBufferPool(ByteBufferPool
the ByteBufferPool of this HttpClient
byteBufferPool
)

Set the ByteBufferPool of this HttpClient.

public void
setConnectBlocking(boolean
whether connect() operations are performed in blocking mode
connectBlocking
)

Whether connect() operations are performed in blocking mode.

public void
setConnectTimeout(long
the max time, in milliseconds, a connection can take to connect to destinations. Zero value means infinite timeout.
connectTimeout
)

public void
setDefaultRequestContentType(String
the default content type for request content
contentType
)

Set the default content type for request content.

public void
setDestinationIdleTimeout(long
the time in ms after which idle destinations are removed
destinationIdleTimeout
)

Whether destinations that have no connections (nor active nor idle) and no exchanges should be removed after the specified timeout.

public void
setExecutor(Executor
the Executor of this HttpClient
executor
)

Set the Executor of this HttpClient.

public void
setFollowRedirects(boolean
whether this HttpClient follows HTTP redirects
follow
)

public void
setHttpCompliance(HttpCompliance
The compliance level which is used to actually parse http responses
httpCompliance
)

Sets the http compliance mode for parsing http responses.

public void
setHttpCookieStore(HttpCookieStore
the cookie store associated with this instance
cookieStore
)

Set the cookie store associated with this instance.

public void
setIdleTimeout(long
the max time, in milliseconds, a connection can be idle (that is, without traffic of bytes in either direction)
idleTimeout
)

Set the max time, in milliseconds, a connection can be idle (that is, without traffic of bytes in either direction).

public void
setMaxConnectionsPerDestination(int
the max number of connections that this HttpClient opens to Destinations
maxConnectionsPerDestination
)

Sets the max number of connections to open to each destinations.

public void
setMaxRedirects(int
the max number of HTTP redirects that are followed in a conversation, or -1 for unlimited redirects
maxRedirects
)

public void
setMaxRequestsQueuedPerDestination(int
the max number of requests that may be queued to a Destination.
maxRequestsQueuedPerDestination
)

Sets the max number of requests that may be queued to a destination.

public void
setMaxResponseHeadersSize(int
the max size in bytes of the response headers
maxResponseHeadersSize
)

Set the max size in bytes of the response headers.

public void
setName(String
the name of this HttpClient
name
)

Sets the name of this HttpClient.

public void
setRequestBufferSize(int
the size of the buffer (in bytes) used to write requests
requestBufferSize
)

Set the size of the buffer (in bytes) used to write requests.

public void
setResponseBufferSize(int
the size of the buffer used to read responses
responseBufferSize
)

Set the size of the buffer used to read responses.

public void
setScheduler(Scheduler
the Scheduler of this HttpClient
scheduler
)

Set the Scheduler of this HttpClient.

public void
setSocketAddressResolver(SocketAddressResolver
the SocketAddressResolver of this HttpClient
resolver
)

Set the SocketAddressResolver of this HttpClient.

public void
setSslContextFactory(SslContextFactory.Client
the SslContextFactory.Client that manages TLS encryption
sslContextFactory
)

Set the SslContextFactory.Client that manages TLS encryption.

public void
setStrictEventOrdering(boolean
whether request/response events must be strictly ordered
strictEventOrdering
)

Whether request/response events must be strictly ordered with respect to connection usage.

public void
setUseInputDirectByteBuffers(boolean
whether to use direct ByteBuffers for reading
useInputDirectByteBuffers
)

Set whether to use direct ByteBuffers for reading.

public void
setUseOutputDirectByteBuffers(boolean
whether to use direct ByteBuffers for writing
useOutputDirectByteBuffers
)

Set whether to use direct ByteBuffers for writing.

public void
setUserAgentField(HttpField
the "User-Agent" HTTP header string of this HttpClient
agent
)

Set the "User-Agent" HTTP header string of this HttpClient.

Inherited from org.eclipse.jetty.util.component.ContainerLifeCycle:
addBeanaddBeanaddEventListeneraddManagedcontainsdestroydumpdumpdumpdumpObjectsdumpStdErrgetBeangetBeansgetBeansgetContainedBeansgetContainedBeansisAutoisManagedisUnmanagedmanageremoveBeanremoveBeansremoveEventListenersetBeansstartstopunmanageupdateBeanupdateBeanupdateBeansupdateBeans