我使用apache commons http客户端通过post发送数据每秒钟,有没有办法让下面的代码更有效率?我知道HTTP是无状态的,但有什么我可以做,以改善因为基础URL总是在这种情况下(仅参数值变化相同。apache commons http客户端效率
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; }
看起来很好。你有没有理由另有想法? – skaffman 2010-06-15 18:33:39