apache commons http client efficiency

Posted by wo_shi_ni_ba_ba on Stack Overflow See other posts from Stack Overflow or by wo_shi_ni_ba_ba
Published on 2010-06-15T18:18:54Z Indexed on 2010/06/15 18:22 UTC
Read the original article Hit count: 209

Filed under:
|

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;
 }

© Stack Overflow or respective owner

Related posts about http

Related posts about client