我使用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