Asynchttpclient is an open source Java asynchronous HTTP and WebSocket client
Official text
Asynchronous Http and WebSocket Client library for Java
Async HTTP Client Library purpose is-to-allow Java applications to easily execute HTTP requests and asynchronously process The HTTP responses. The library also supports the WebSocket Protocol. The Async HTTP Client Library is simple-to-use. Tutorials maven Dependency
<dependency>
<groupId>com.ning</groupId>
<artifactid>async-http-client</ artifactid>
<version>1.9.39</version>
</dependency>
GET Request
Import com.ning.http.client.*;
Import Java.util.concurrent.Future;
public string get (string url) {
asynchttpclient asynchttpclient = new Asynchttpclient ();
future<response> f = asynchttpclient.prepareget (URL). Execute ();
Response r = F.get ();
return R.getresponsebody ();
}
in the handler, processing response
public string get (string url) {
asynchttpclient asynchttpclient = new Asynchttpclient ();
future<response> f = asynchttpclient.prepareget (URL). Execute (new asynccompletionhandler<response> () {
@Override public
Response oncompleted (Response Response) throws exception{
//does something with the Response
//...
return response;
}
@Override public
void onthrowable (Throwable t) {
//Something wrong happened.
}}
);
Response r = F.get ();
return R.getresponsebody ();
}
POST Request
public string post (string url, list<formparam> params) {
list<param> formparams = new Arraylist<param > ();
for (Formparam param:params) {
formparams.add (new param (Param.getname (), Param.getvalue ()));
}
Asynchttpclient asynchttpclient = new Asynchttpclient ();
future<response> f = asynchttpclient.preparepost (URL). Setformparams (Formparams). Execute ();
Response r = F.get ();
return R.getresponsebody ();
}
Configure Asynchttpclient
Asynchttpclientconfig CF = new Asynchttpclientconfig.builder ()
. Setproxyserver (New ProxyServer ("127.0.0.1", 38080)
. Setconnecttimeout (6000).
setreadtimeout (6000).
setmaxconnections (+)
. Setmaxconnectionsperhost (
). build ();
Asynchttpclient asynchttpclient = new asynchttpclient (CF);
Best Practices
Asynchttpclientimpl class
Package Com.bytebeats.toolkit.http.impl;
Import Com.bytebeats.toolkit.annotation.ThreadSafe;
Import com.bytebeats.toolkit.http.HttpRequestException;
Import Com.bytebeats.toolkit.http.config.FormParam;
Import Com.bytebeats.toolkit.http.config.HttpRequestConfig;
Import com.ning.http.client.AsyncHttpClient;
Import Com.ning.http.client.AsyncHttpClientConfig;
Import Com.ning.http.client.Param;
Import Com.ning.http.client.Response;
Import java.io.IOException;
Import java.util.ArrayList;
Import java.util.List;
Import java.util.concurrent.ExecutionException;
Import Java.util.concurrent.Future; /** * Asynchttpclient Implementation * https://github.com/AsyncHttpClient/async-http-client/tree/1.9.x * * @author Ricky Fung * @ Create 2016-08-23 12:18 */@ThreadSafe public class Asynchttpclientimpl extends HttpClient {private asynchttpclient
Asynchttpclient;
Public Asynchttpclientimpl (httprequestconfig config) {super (config); Asynchttpclientconfig CF = new AsynchttpclientconfiG.builder ()//.setproxyserver (New ProxyServer ("127.0.0.1", 38080)). Setconnecttimeout (This . Config.getconnecttimeout ()). Setreadtimeout (This.config.getReadTimeout ()). Setmaxconnecti
ONS (This.config.getMaxConnections ()). Setmaxconnectionsperhost (This.config.getMaxConnectionPerHost ())
. build ();
Asynchttpclient = new Asynchttpclient (CF); } @Override public string get (string url) throws Httprequestexception {try {Response r = pr
Epareget (asynchttpclient, URL);
return R.getresponsebody ();
} catch (Interruptedexception e) {throw new Httprequestexception ("Asynchttpclient get Error", E);
} catch (Executionexception e) {throw new Httprequestexception ("Asynchttpclient get Error", E);
} catch (IOException e) {throw new Httprequestexception ("Asynchttpclient get Error", E);
}
} @Override public byte[] getBytes (String URL) throws Httprequestexception {try {Response r =
Prepareget (asynchttpclient, URL);
return R.getresponsebodyasbytes ();
} catch (Interruptedexception e) {throw new Httprequestexception ("Asynchttpclient getBytes error", E);
} catch (Executionexception e) {throw new Httprequestexception ("Asynchttpclient getBytes error", E);
} catch (IOException e) {throw new Httprequestexception ("Asynchttpclient getBytes error", E); }} @Override public string postwithform (string url, list<formparam> params) throws Httprequestexceptio
n {try {Response R = preparepostform (URL, params);
return R.getresponsebody ();
} catch (IOException e) {throw new Httprequestexception ("Asynchttpclient postwithform error", E); } catch (Interruptedexception e) {throw new HttprequeStexception ("Asynchttpclient postwithform error", E);
} catch (Executionexception e) {throw new Httprequestexception ("Asynchttpclient postwithform error", E); }} @Override public byte[] postwithformbytes (String URL, list<formparam> params) throws Httpreque
stexception {try {Response R = preparepostform (URL, params);
return R.getresponsebodyasbytes ();
} catch (IOException e) {throw new Httprequestexception ("Asynchttpclient postwithformbytes error", E); } catch (Interruptedexception e) {throw new Httprequestexception ("Asynchttpclient postwithformbytes Error"
, e);
} catch (Executionexception e) {throw new Httprequestexception ("Asynchttpclient postwithformbytes error", E);
}} @Override public string postwithbody (string URL, string data) throws Httprequestexception { try {Response r = preparepostbody (URL,data);
return R.getresponsebody ();
} catch (IOException e) {throw new Httprequestexception ("Asynchttpclient postwithbody error", E);
} catch (Interruptedexception e) {throw new Httprequestexception ("Asynchttpclient postwithbody error", E); } catch (Executionexception e) {throw new Httprequestexception ("Asynchttpclient postwithbody error", E
);
}} @Override public byte[] postwithbodybytes (string URL, string data) throws Httprequestexception {
try {Response r = preparepostbody (URL, data);
return R.getresponsebodyasbytes ();
} catch (IOException e) {throw new Httprequestexception ("Asynchttpclient postwithbodybytes error", E); } catch (Interruptedexception e) {throw new Httprequestexception ("Asynchttpclient postwithbodybytes Error"
, e); } catch (Executionexception e) {throw new Httprequestexception ("asYnchttpclient postwithbodybytes Error ", E);
}} public Response preparepostbody (string URL, string data) throws Executionexception, Interruptedexception { future<response> f = asynchttpclient.preparepost (URL). Setbody (data). AddHeader ("Content-type", "application/
JSON "). Execute ();
return F.get (); } public Response preparepostform (String URL, list<formparam> params) throws Executionexception, Interruptedexc
eption {list<param> formparams = new arraylist<param> ();
for (Formparam param:params) {formparams.add (new param (Param.getname (), Param.getvalue ()));
} future<response> f = asynchttpclient.preparepost (URL). Setformparams (Formparams). Execute ();
return F.get (); } public Response Prepareget (asynchttpclient asynchttpclient, String url) throws Executionexception, Interruptedexcep tion {future<response> f = asynchttpclient.prepareget (URL). Execute ();
Response r = F.get ();
return R;
}
}
Formparam class
Package com.bytebeats.toolkit.http.config;
public class Formparam {
private final String name;
Private final String value;
Public Formparam (string name, String value) {
this.name = name;
This.value = value;
}
Public String GetName () {
return this.name;
}
Public String GetValue () {
return this.value;
}
}
Httprequestconfig class
Package com.bytebeats.toolkit.http.config; /** * ${description} * * @author Ricky Fung * @create 2016-08-24 19:06 * public class Httprequestconfig {Publi
C static final Httprequestconfig DEFAULT = new Httprequestconfig ();
private int readtimeout = 6000;
private int sockettimeout = 6000;
private int connecttimeout = 6000;
private int connectionrequesttimeout = 6000;
private int maxconnections = 200;
private int maxconnectionperhost = 50;
Private Boolean requestretryenabled;
Private Boolean RetryCount;
Proxy server private Httphost proxy;
public int getreadtimeout () {return readtimeout;
} public void Setreadtimeout (int readtimeout) {this.readtimeout = ReadTimeout;
} public int Getsockettimeout () {return sockettimeout;
} public void SetSocketTimeout (int sockettimeout) {this.sockettimeout = Sockettimeout; } public int Getconnecttimeout () {return ConnecttiMeout;
} public void Setconnecttimeout (int connecttimeout) {this.connecttimeout = ConnectTimeout;
} public int Getconnectionrequesttimeout () {return connectionrequesttimeout; } public void Setconnectionrequesttimeout (int connectionrequesttimeout) {this.connectionrequesttimeout = con
Nectionrequesttimeout;
} public int Getmaxconnectionperhost () {return maxconnectionperhost; } public void Setmaxconnectionperhost (int maxconnectionperhost) {this.maxconnectionperhost = Maxconnectionpe
RHost;
} public boolean isrequestretryenabled () {return requestretryenabled; } public void Setrequestretryenabled (Boolean requestretryenabled) {this.requestretryenabled = Requestretryen
abled;
} public boolean Isretrycount () {return retrycount;
The public void Setretrycount (Boolean retrycount) {this.retrycount = RetryCount; } public int GetmaxconnEctions () {return maxconnections;
} public void setmaxconnections (int maxconnections) {this.maxconnections = MaxConnections;
} public Httphost GetProxy () {return proxy;
} public void SetProxy (Httphost proxy) {this.proxy = proxy;
}
}
References
Https://github.com/AsyncHttpClient/async-http-client
https://github.com/AsyncHttpClient/async-http-client/tree/1.7.x