java.lang.Object | |
↳ | com.ning.http.client.AsyncHttpClient |
This class support asynchronous and synchronous HTTP request.
To execute synchronous HTTP request, you just need to doThe code above will block until the response is fully received. To execute asynchronous HTTP request, you create anAsyncHttpClient c = new AsyncHttpClient(); Futuref = c.prepareGet("http://www.ning.com/").execute();
AsyncHandler
or its abstract implementation, AsyncCompletionHandler
onCompleted(com.ning.http.client.Response) will be invoked once the http response has been fully read, which include the http headers and the response body. Note that the entire response will be buffered in memory. You can also have more control about the how the response is asynchronously processed by using aAsyncHttpClient c = new AsyncHttpClient(); Futuref = c.prepareGet("http://www.ning.com/").execute(new AsyncCompletionHandler () { @Override public Response onCompleted(Response response) throws IOException { // Do something return response; } @Override public void onThrowable(Throwable t) { } }); Response response = f.get(); // We are just interested to retrieve the status code. Future f = c.prepareGet("http://www.ning.com/").execute(new AsyncCompletionHandler () { @Override public Integer onCompleted(Response response) throws IOException { // Do something return response.getStatusCode(); } @Override public void onThrowable(Throwable t) { } }); Integer statusCode = f.get();
AsyncHandler
HttpContent sub classes, you can asynchronously process the response status,headers and body and decide when to stop the processing the response by throwing a new {link ResponseComplete} at any moment. This class can also be used without the need ofAsyncHttpClient c = new AsyncHttpClient(); Futuref = c.prepareGet("http://www.ning.com/").execute(new AsyncHandler () { private StringBuilder builder = new StringBuilder(); @Override public STATE onStatusReceived(HttpResponseStatus s) throws Exception { // return STATE.CONTINUE or STATE.ABORT return STATE.CONTINUE } @Override public STATE onHeadersReceived(HttpResponseHeaders bodyPart) throws Exception { // return STATE.CONTINUE or STATE.ABORT return STATE.CONTINUE } @Override public STATE onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception { builder.append(new String(bodyPart)); // return STATE.CONTINUE or STATE.ABORT return STATE.CONTINUE } @Override public String onCompleted() throws Exception { // Will be invoked once the response has been fully read or a ResponseComplete exception // has been thrown. return builder.toString(); } @Override public void onThrowable(Throwable t) { } }); String bodyResponse = f.get();
AsyncHandler
Finally, you can configure the AsyncHttpClient using anAsyncHttpClient c = new AsyncHttpClient(); Futuref = c.prepareGet(TARGET_URL).execute(); Response r = f.get();
AsyncHttpClientConfig
instance
An instance of this class will cache every HTTP 1.1 connections and close them when theAsyncHttpClient c = new AsyncHttpClient(new AsyncHttpClientConfig.Builder().setRequestTimeoutInMs(...).build()); Futuref = c.prepareGet(TARGET_URL).execute(); Response r = f.get();
getIdleConnectionTimeoutInMs()
expires. This object can hold many persistent connections to different host.
Nested Classes | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
AsyncHttpClient.BoundRequestBuilder |
Fields | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
signatureCalculator | Default signature calculator to use for all requests constructed by this client instance. |
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Create a new HTTP Asynchronous Client using the default
AsyncHttpClientConfig configuration. | |||||||||||
Create a new HTTP Asynchronous Client using a
AsyncHttpClientConfig configuration and the
DEFAULT_PROVIDER | |||||||||||
Create a new HTTP Asynchronous Client using an implementation of
AsyncHttpProvider and
the default AsyncHttpClientConfig configuration. | |||||||||||
Create a new HTTP Asynchronous Client using a
AsyncHttpClientConfig configuration and
and a AsyncHttpProvider . | |||||||||||
Create a new HTTP Asynchronous Client using a
AsyncHttpClientConfig configuration and
and a AsyncHttpProvider class' name. |
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Close the underlying connections.
| |||||||||||
Execute an HTTP request.
| |||||||||||
Execute an HTTP request.
| |||||||||||
Return the
AsyncHttpClientConfig | |||||||||||
Return the asynchronous
AsyncHttpProvider | |||||||||||
Return true if closed
| |||||||||||
Prepare an HTTP client CONNECT request.
| |||||||||||
Prepare an HTTP client DELETE request.
| |||||||||||
Prepare an HTTP client GET request.
| |||||||||||
Prepare an HTTP client HEAD request.
| |||||||||||
Prepare an HTTP client OPTIONS request.
| |||||||||||
Prepare an HTTP client POST request.
| |||||||||||
Prepare an HTTP client PUT request.
| |||||||||||
Construct a
AsyncHttpClient.BoundRequestBuilder using a Request | |||||||||||
Set default signature calculator to use for requests build by this client instance
|
Protected Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
![]() |
Default signature calculator to use for all requests constructed by this client instance.
Create a new HTTP Asynchronous Client using the default AsyncHttpClientConfig
configuration. The
default AsyncHttpProvider
will be used (NettyAsyncHttpProvider
Create a new HTTP Asynchronous Client using a AsyncHttpClientConfig
configuration and the
DEFAULT_PROVIDER
config | a AsyncHttpClientConfig
|
---|
Create a new HTTP Asynchronous Client using an implementation of AsyncHttpProvider
and
the default AsyncHttpClientConfig
configuration.
provider | a AsyncHttpProvider
|
---|
Create a new HTTP Asynchronous Client using a AsyncHttpClientConfig
configuration and
and a AsyncHttpProvider
.
httpProvider | a AsyncHttpProvider
|
---|---|
config | a AsyncHttpClientConfig |
Create a new HTTP Asynchronous Client using a AsyncHttpClientConfig
configuration and
and a AsyncHttpProvider class' name.
providerClass | a AsyncHttpProvider
|
---|---|
config | a AsyncHttpClientConfig |
Close the underlying connections.
Execute an HTTP request.
request | Request |
---|
IOException |
---|
Execute an HTTP request.
request | Request |
---|---|
handler | an instance of AsyncHandler |
IOException |
---|
Return true if closed
Prepare an HTTP client CONNECT request.
url | A well formed URL. |
---|
Prepare an HTTP client DELETE request.
url | A well formed URL. |
---|
Prepare an HTTP client GET request.
url | A well formed URL. |
---|
Prepare an HTTP client HEAD request.
url | A well formed URL. |
---|
Prepare an HTTP client OPTIONS request.
url | A well formed URL. |
---|
Prepare an HTTP client POST request.
url | A well formed URL. |
---|
Prepare an HTTP client PUT request.
url | A well formed URL. |
---|
Construct a AsyncHttpClient.BoundRequestBuilder
using a Request
request | a Request |
---|
Set default signature calculator to use for requests build by this client instance
Throwable |
---|