首页 文章

apache commons http客户端效率

提问于
浏览
4

我使用apache commons http客户端每秒通过post发送数据,有没有办法让下面的代码更有效率?我知道http是无状态的,但是我可以做些什么来改进,因为在这种情况下基本URL总是相同的(只有参数值改变 .

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

2 回答

相关问题