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();
}
當我運行這個程序時,我得到了foll異常。
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塊中的連接。正如在aonther線程中所建議的,我在重新使用連接之前已經使用了響應主體。這裏還會出現什麼問題?
你調用'的execute()'兩次在同一個'DefaultHttpClient'和它告訴你,你不能這樣做,顯然是。 – 2012-01-10 17:15:41