使用org.apache.http.protocol.HttpRequestExecutor.execute时“无法打开连接”?

时间:2011-03-10 22:17:08

标签: java

我正在尝试打开连接:

HttpPost hp = new HttpPost();
HttpParams rp = new BasicHttpParams();
HttpClientConnection hc = new DefaultHttpClientConnection();
HttpContext hctx = new BasicHttpContext();

hp.setURI(new URI(baseUrl + "/login.jsp"));
rp.setParameter("os_username", username);
rp.setParameter("os_password", password);
rp.setParameter("os_destination", "/secure");
hp.setParams(rp);

HttpResponse response = httpexecutor.execute(hp, hc, hctx);

但是,我得到了这个堆栈跟踪:

java.lang.IllegalStateException: Connection is not open
    at org.apache.http.impl.SocketHttpClientConnection.assertOpen(SocketHttpClientConnection.java:76)
    at org.apache.http.impl.AbstractHttpClientConnection.sendRequestHeader(AbstractHttpClientConnection.java:239)
    at org.apache.http.protocol.HttpRequestExecutor.doSendRequest(HttpRequestExecutor.java:213)
    at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:124)

什么可能导致“连接未打开”?我没有看到任何可用于强制打开连接的特定方法。

1 个答案:

答案 0 :(得分:0)

我记得这需要包围:

try{
...code...
} catch (Exception e) {
out.println(e.getLocalizedMessage()); //or System.out.println
}

希望这有帮助。