使用HttpClient在POST后执行GET时出现异常
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为在 POST 之后,连接管理器仍然保留着 POST 响应连接。您需要先将其释放,然后才能将客户端用于其他用途。
这应该可行:
然后,您可以执行 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:
Then, you can execute your GET.