使用HttpClient在POST后执行GET时出现异常

发布于 2024-10-12 07:35:45 字数 1496 浏览 8 评论 0原文

我使用 Apache 的 DefaultHttpClient()execute(HttpPost post) 方法来进行 http POST。 这样我就登录到了一个网站。 然后我想使用同一个客户端来创建一个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();


}

I use Apache's DefaultHttpClient() with the execute(HttpPost post) method to make a http POST.
With this I log on to a website.
Then I want to use the same Client to make a HttpGet.
But when I do, I get an Exception:

Exception in thread "main" java.lang.IllegalStateException: Invalid use of SingleClientConnManager: connection still allocated.

I am not sure as to why this occurs. Any help would be appreciated.

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();


}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拥有 2024-10-19 07:35:45

这是因为在 POST 之后,连接管理器仍然保留着 POST 响应连接。您需要先将其释放,然后才能将客户端用于其他用途。

这应该可行:

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

然后,您可以执行 GET。

This is because after the POST, the connection manager is still holding on to the POST response connection. You need to make it release that before you can use the client for something else.

This should work:

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

Then, you can execute your GET.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文