apache commons http client efficiency
- by wo_shi_ni_ba_ba
I use apache commons http client to send data via post every second, is there a way to make the following code more efficient? I know http is stateless, but is there anything I can do to improve since the base url is always the same in this case(only the parameter value change.
private void sendData(String s){
try
{
HttpClient client = getHttpClient();
HttpPost method = new HttpPost("http://192.168.1.100:8080/myapp");
System.err.println("send to server "+s);
List formparams = new ArrayList();
formparams.add(new BasicNameValuePair("packet", s));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
method.setEntity(entity);
HttpResponse resp=client.execute(method);
String res = EntityUtils.toString(resp.getEntity());
System.out.println(res);
}
catch (Exception e)
{
e.printStackTrace();
}
}
private HttpClient getHttpClient() {
if(httpClient==null){
httpClient = new DefaultHttpClient();
}
return httpClient;
}