2015-11-07 52 views
0

我有一個應用程序部署在使用Apache HTTPClient的谷歌應用程序引擎上。最近,隨着應用獲得更多流量,我已經開始遇到超出套接字配額的例外情況。唯一的例外是我的HTTPClient連接是否泄露套接字?

com.google.apphosting.api.ApiProxy$OverQuotaException: The API call remote_socket.SetSocketOptions() required more quota than is available. 

我伸手到App Engine團隊,他們要我檢查我的應用程序漏水插座。

CloseableHttpClient httpclient = HttpClients.createDefault(); 

HttpPost httpPost = new HttpPost("http://www.spark.com"); 

List <NameValuePair> nvps = new ArrayList <NameValuePair>(); 

nvps.add(new BasicNameValuePair("param1", "val1")); 
nvps.add(new BasicNameValuePair("param2", "val2")); 

httpPost.setEntity(new UrlEncodedFormEntity(nvps)); 

CloseableHttpResponse response = httpclient.execute(httpPost); 
Document doc = null; 
try { 
    HttpEntity entity = response.getEntity(); 
    doc = Jsoup.parse(entity.getContent(), "UTF-8", ""); 
    EntityUtils.consume(entity); 
} finally { 
    response.close(); 
    httpclient.close(); 
} 

這就是我的http連接代碼的樣子。我是否做了可能導致套接字泄漏的錯誤?我可以做更好的事嗎?

回答

0

這個工作對我來說:

HttpParams httpParameters = new BasicHttpParams(); 
    HttpProtocolParams.setContentCharset(httpParameters, HTTP.UTF_8); 
    HttpProtocolParams.setHttpElementCharset(httpParameters, HTTP.UTF_8); 

     HttpClient httpclient = new DefaultHttpClient(httpParameters); 
     // HttpPost httppost = new HttpPost("http://rafsanjan.uni-azad.my.com/json/darkhasr.php?shdaneshjo="+value_id+"&moavenat="+value_seaction+"&darkhast="+zir_item+"&startdate=test&tozih="+ value_descration); //??? 
     try { 
      URIBuilder builder = new URIBuilder(); 
      builder.setScheme("http") 
        .setHost("app.my.ac.com") 
        .setPort(1180) 
        .setPath("/json2/darkhasr.php") 
        .addParameter("shdaneshjo", value_id) 
        .addParameter("moavenat", value_seaction) 
        .addParameter("darkhast", value_item) 
        .addParameter("startdatet", "0") 
        .addParameter("tozih", value_descration) 
        .build(); 
      // .fragment("section-name"); 
      String myUrl = builder.toString(); 
      Log.d("url=>",myUrl); 

      HttpPost httppost = new HttpPost(myUrl); 

      ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(8); 
      //nameValuePairs.add(new BasicNameValuePair("name", name)); 
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8")); 
      HttpResponse response = httpclient.execute(httppost); 
      HttpEntity resEntity = response.getEntity(); 
      Log.d("RESPONSE",EntityUtils.toString(resEntity)); 

     } 
     catch(Exception e) 
     { 
      Log.e("log_tag", "Error: "+e.toString()); 
     }