public class HttpClient extends ContainerLifeCycle
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
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 getCookieStore()
.
Typical usage:
// One liner: new HttpClient().GET("http://localhost:8080/").get().status(); // Building a request with a timeout HttpClient client = new HttpClient(); Response response = client.newRequest("http://localhost:8080").send().get(5, TimeUnit.SECONDS); int status = response.status(); // Asynchronously HttpClient client = new HttpClient(); client.newRequest("http://localhost:8080").send(new Response.Listener.Adapter() { @Override public void onSuccess(Response response) { ... } });
Modifier and Type | Class and Description |
---|---|
protected class |
HttpClient.ClientSelectorManager |
AbstractLifeCycle.AbstractLifeCycleListener
Container.InheritedListener, Container.Listener
LifeCycle.Listener
Constructor and Description |
---|
HttpClient() |
HttpClient(SslContextFactory sslContextFactory) |
addBean, addBean, addBean, contains, destroy, dump, dump, dump, dump, dumpBeans, dumpObject, dumpStdErr, dumpThis, getBean, getBeans, getBeans, isManaged, manage, removeBean, removeBeans, setStopTimeout, unmanage, updateBean, updateBeans
addLifeCycleListener, getState, getState, getStopTimeout, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, removeLifeCycleListener, start, stop
public HttpClient()
public HttpClient(SslContextFactory sslContextFactory)
public SslContextFactory getSslContextFactory()
protected void doStart() throws Exception
ContainerLifeCycle
doStart
in class ContainerLifeCycle
Exception
protected SelectorManager newSelectorManager()
protected void doStop() throws Exception
ContainerLifeCycle
doStop
in class ContainerLifeCycle
Exception
public List<Request.Listener> getRequestListeners()
public CookieStore getCookieStore()
public AuthenticationStore getAuthenticationStore()
public Set<ContentDecoder.Factory> getContentDecoderFactories()
public Future<ContentResponse> GET(String uri)
public Future<ContentResponse> GET(URI uri)
public Destination getDestination(String scheme, String host, int port)
public List<Destination> getDestinations()
protected void send(Request request, Response.Listener listener)
protected void newConnection(HttpDestination destination, Callback<Connection> callback)
protected void configure(SocketChannel channel) throws SocketException
SocketException
protected HttpConversation getConversation(long id)
protected void removeConversation(HttpConversation conversation)
protected List<ProtocolHandler> getProtocolHandlers()
protected ProtocolHandler findProtocolHandler(Request request, Response response)
public ByteBufferPool getByteBufferPool()
public void setByteBufferPool(ByteBufferPool byteBufferPool)
public long getIdleTimeout()
public void setIdleTimeout(long idleTimeout)
public SocketAddress getBindAddress()
setBindAddress(SocketAddress)
public void setBindAddress(SocketAddress bindAddress)
bindAddress
- the address to bind socket channels togetBindAddress()
public String getUserAgent()
public void setUserAgent(String agent)
public boolean isFollowRedirects()
public void setFollowRedirects(boolean follow)
public Executor getExecutor()
public void setExecutor(Executor executor)
public Scheduler getScheduler()
public void setScheduler(Scheduler scheduler)
public SelectorManager getSelectorManager()
public int getMaxConnectionsPerAddress()
public void setMaxConnectionsPerAddress(int maxConnectionsPerAddress)
public int getMaxQueueSizePerAddress()
public void setMaxQueueSizePerAddress(int maxQueueSizePerAddress)
public int getRequestBufferSize()
public void setRequestBufferSize(int requestBufferSize)
public int getResponseBufferSize()
public void setResponseBufferSize(int responseBufferSize)
public int getMaxRedirects()
public void setMaxRedirects(int maxRedirects)
public boolean isTCPNoDelay()
public void setTCPNoDelay(boolean tcpNoDelay)
public boolean isDispatchIO()
setDispatchIO(boolean)
public void setDispatchIO(boolean dispatchIO)
dispatchIO
must be set to true.
If application callbacks are known to be quick and never block on I/O, then parameter dispatchIO
may be set to false.dispatchIO
- true to dispatch I/O operations in a different thread, false to execute them in the selector threadpublic void dump(Appendable out, String indent) throws IOException
dump
in interface Dumpable
dump
in class ContainerLifeCycle
IOException
Copyright © 1995-2012 Mort Bay Consulting. All Rights Reserved.