2012-01-10 8 views
0

ページの応答を読み取るコードがあります。それはこのように見えます。例外--- SingleClientConnManager:まだ接続が割り当て済み

public static void main(String[] args) 
    { 
     DefaultHttpClient httpclient = new DefaultHttpClient(); 
     HttpEntity entity = null; 
     try{ 

      String it= "https://www.aaa/login"; 
       HttpPost httpost = new HttpPost(it); 

       List <NameValuePair> ln= new ArrayList <NameValuePair>(); 
       ln.add(new BasicNameValuePair("un", "aaa")); 
       ln.add(new BasicNameValuePair("pwd", "bbb")); 
       httpost.setEntity(new UrlEncodedFormEntity(ln, HTTP.UTF_8)); 

       HttpResponse response = httpclient.execute(httpost); 
       entity = response.getEntity(); 
       GZIPInputStream gis= new GZIPInputStream(entity.getContent()); 
      StringBuffer st = new StringBuffer(); 
       for (int c = gis.read(); c != -1; c = gis.read()) { 
        st.append((char)c); 
       } 

        CookieStore cookieStore = httpclient.getCookieStore(); 
        HttpContext httpContext = new BasicHttpContext(); 
        httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); 
        HttpGet httpget = new HttpGet("http://aaa/index"); 
        HttpResponse httpResponse = httpclient.execute(httpget, httpContext); 
        entity = httpResponse.getEntity(); 

        if (entity != null) { 
         BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent())); 
         String readLine; 
         while(((readLine = br.readLine()) != null)) { 
          System.err.println("br :"+readLine); 
        } 


       }     
     }catch(Exception e){ 
      e.printStackTrace(); 
     } 
     finally{ 

      EntityUtils.consume(entity); 
      httpclient.getConnectionManager().shutdown(); 
     } 

このプログラムを実行すると、例外が発生します。

java.lang.IllegalStateException: Invalid use of SingleClientConnManager: connection still allocated. 
Make sure to release the connection before allocating another one. 
    at org.apache.http.impl.conn.SingleClientConnManager.getConnection(SingleClientConnManager.java:216) 
    at org.apache.http.impl.conn.SingleClientConnManager$1.getConnection(SingleClientConnManager.java:190) 
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:401) 
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:820) 
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:754) 

finallyブロックで接続を閉じました。別のスレッドで提案されているように、私は接続を再利用する前に応答本体を消費しました。ここで何が間違っていますか?

+2

が必要。 –

答えて

2

HttpResponse httpResponse = httpclient.execute(httpget, httpContext); 

前にも、あなたが同じ `DefaultHttpClient`に二回`)(実行 `呼んでいると、明らかに、あなたがそれを行うことができないことを伝えるだ

EntityUtils.consume(entity); 
関連する問題