3

DefaultHttpClient()httpPOSTを作成するexecute(HttpPost post)メソッドでApacheを使用します。これで私はウェブサイトにログオンします。次に、同じクライアントを使用してを作成しHttpGetます。しかし、そうすると、例外が発生します。

スレッド"main"の例外java.lang.IllegalStateException:SingleClientConnManagerの無効な使用:接続はまだ割り当てられています。

なぜこれが起こるのかわかりません。どんな助けでもいただければ幸いです。

public static void main(String[] args) throws Exception {

    // prepare post method
    HttpPost post = new HttpPost("http://epaper02.niedersachsen.com/epaper/index_GT_neu.html");

    // add parameters to the post method
    List <NameValuePair> parameters = new ArrayList <NameValuePair>();
    parameters.add(new BasicNameValuePair("username", "test"));
    parameters.add(new BasicNameValuePair("passwort", "test")); 

    UrlEncodedFormEntity sendentity = new UrlEncodedFormEntity(parameters, HTTP.UTF_8);
    post.setEntity(sendentity); 

    // create the client and execute the post method
    HttpClient client = new DefaultHttpClient();
    HttpResponse postResponse = client.execute(post);
    //Use same client to make GET (This is where exception occurs)
    HttpGet httpget = new HttpGet(PDF_URL);
    HttpContext context = new BasicHttpContext();

    HttpResponse getResponse = client.execute(httpget, context);



    // retrieve the output and display it in console
    System.out.print(convertInputStreamToString(postResponse.getEntity().getContent()));
    client.getConnectionManager().shutdown();


}
4

1 に答える 1

2

これは、POST後も、接続マネージャーがPOST応答接続を保持しているためです。クライアントを他の目的で使用する前に、それをリリースする必要があります。

これは機能するはずです:

HttpResponse postResponse = client.execute(post);
EntityUtils.consume(postResponse.getEntity();

次に、GETを実行できます。

于 2011-01-15T14:07:59.360 に答える