[英]Invalid use of SingleClientConnManager: connection still allocated
[英]Exception---SingleClientConnManager: connection still allocated
我有一個代碼來讀取頁面的響應。 它看起來像這樣。
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塊中的連接。 正如在其他線程中所建議的那樣,我在重用連接之前已經使用了響應主體。 還有什么會出錯?
之前
HttpResponse httpResponse = httpclient.execute(httpget, httpContext);
你還需要
EntityUtils.consume(entity);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.