HttpClient 4.2.3 - 连接重置异常

时间:2013-04-19 19:43:47

标签: java apache java-ee apache-httpclient-4.x

我正在开发一个应用程序,使用Apache HttpClient 4.2.3发送带URL参数的https POST请求。我必须在POST请求中发送URL参数,如“?username = abc”。我正在获得连接重置异常。我不知道为解决这个问题需要解决的问题。如果有人帮我解决这个问题,我会很高兴的。

“httpClient.execute(httpPost);”时抛出“连接重置”异常代码被执行。

以下是获取httpClient的代码:

    .  .. . . . . .. . . . .
    System.out.println("--Enter HttpClient.getSSLHttpClient--");

    // a trust manager that trusts everything
    X509TrustManager allTrustManager = new AllTrustManager();

    // SSLContext
    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(null, new TrustManager[] { allTrustManager }, null);

    SSLSocketFactory sslSocketFactory = new SSLSocketFactory(sslContext);
    Scheme httpsScheme = new Scheme("https", port,sslSocketFactory);
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(httpsScheme);

    ClientConnectionManager cm = new BasicClientConnectionManager(schemeRegistry);

    //System.out.println("--Exit HttpClient.getSSLHttpClient--");

    DefaultHttpClient httpClient = new DefaultHttpClient(cm,getHttpParams());

    System.out.println("--Exit HttpClient.getSSLHttpClient--");

    return httpClient;  
    .  .. . . . . .. . . . .



Here is the stake trace:

11:58:27.593 [main] DEBUG o.a.h.i.c.BasicClientConnectionManager - Get connection for route {s}->https://xyz.com:443
11:58:27.786 [main] DEBUG o.a.h.i.c.DefaultClientConnectionOperator - Connecting to xyz.com:443
11:58:29.379 [main] DEBUG o.a.h.c.protocol.RequestAddCookies - CookieSpec selected: best-match
11:58:29.387 [main] DEBUG o.a.h.c.protocol.RequestAuthCache - Auth cache not set in the context
11:58:29.387 [main] DEBUG o.a.h.c.p.RequestTargetAuthentication - Target auth state: UNCHALLENGED
11:58:29.388 [main] DEBUG o.a.h.c.p.RequestProxyAuthentication - Proxy auth state: UNCHALLENGED
11:58:29.388 [main] DEBUG o.a.h.impl.client.DefaultHttpClient - Attempt 1 to execute request
11:58:29.388 [main] DEBUG o.a.h.i.conn.DefaultClientConnection - Sending request: POST /user?username=abc&password=123 HTTP/1.1
11:58:29.388 [main] DEBUG org.apache.http.wire - >> "POST /user?username=abc&password=123 HTTP/1.1[\r][\n]"
11:58:29.389 [main] DEBUG org.apache.http.wire - >> "Keep-Alive: true[\r][\n]"
11:58:29.389 [main] DEBUG org.apache.http.wire - >> "Accept: text/html[\r][\n]"
11:58:29.389 [main] DEBUG org.apache.http.wire - >> "Content-Length: 0[\r][\n]"
11:58:29.389 [main] DEBUG org.apache.http.wire - >> "Host: xyz.com:443[\r][\n]"
11:58:29.389 [main] DEBUG org.apache.http.wire - >> "Connection: Keep-Alive[\r][\n]"
11:58:29.389 [main] DEBUG org.apache.http.wire - >> "[\r][\n]"
11:58:29.389 [main] DEBUG org.apache.http.headers - >> POST /user?username=abc&password=123 HTTP/1.1
11:58:29.389 [main] DEBUG org.apache.http.headers - >> Keep-Alive: true
11:58:29.389 [main] DEBUG org.apache.http.headers - >> Accept: text/html
11:58:29.389 [main] DEBUG org.apache.http.headers - >> Content-Length: 0
11:58:29.390 [main] DEBUG org.apache.http.headers - >> Host: xyz.com:443
11:58:29.390 [main] DEBUG org.apache.http.headers - >> Connection: Keep-Alive
11:58:29.660 [main] DEBUG o.a.h.i.conn.DefaultClientConnection - Connection 0.0.0.0:53007<->xx.xx.xx.xx:443 closed
11:58:29.660 [main] DEBUG o.a.h.impl.client.DefaultHttpClient - Closing the connection.
11:58:29.660 [main] DEBUG o.a.h.i.conn.DefaultClientConnection - Connection 0.0.0.0:53007<->xx.xx.xx.xx:443 closed
11:58:29.660 [main] DEBUG o.a.h.i.conn.DefaultClientConnection - Connection 0.0.0.0:53007<->xx.xx.xx.xx:443 shut down
11:58:29.661 [main] DEBUG o.a.h.i.c.BasicClientConnectionManager - Releasing connection org.apache.http.impl.conn.ManagedClientConnectionImpl@4f8bc5eb
11:58:29.661 [main] DEBUG o.a.h.i.c.BasicClientConnectionManager - Connection can be kept alive for 9223372036854775807 MILLISECONDS
java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(SocketInputStream.java:189)
    at java.net.SocketInputStream.read(SocketInputStream.java:121)
    at sun.security.ssl.InputRecord.readFully(InputRecord.java:312)
    at sun.security.ssl.InputRecord.read(InputRecord.java:350)
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:927)
    at sun.security.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:884)
    at sun.security.ssl.AppInputStream.read(AppInputStream.java:102)
    at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:166)
    at org.apache.http.impl.io.SocketInputBuffer.fillBuffer(SocketInputBuffer.java:90)
    at org.apache.http.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.java:281)
    at org.apache.http.impl.conn.LoggingSessionInputBuffer.readLine(LoggingSessionInputBuffer.java:115)
    at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:92)
    at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:62)
    at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:254)
    at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:289)
    at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:252)
    at org.apache.http.impl.conn.ManagedClientConnectionImpl.receiveResponseHeader(ManagedClientConnectionImpl.java:191)
    at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:300)
    at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:127)
    at org.apache.http.impl.client.DefaultRequestDirector.tryExecute(DefaultRequestDirector.java:717)
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:522)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:784)

2 个答案:

答案 0 :(得分:1)

最后,我找到了解决方案。我提到过我使用过HttpClient 4.2.3。以前,我使用以下代码来获取HttpPost对象:

HttpPost httpPost = new HttpPost("https://xyz.com:443/user?username=abc&password=123");
.. . .. . .
httpClient.execute(httpPost);
..  .. . . . . 

并获得“连接重置”例外。

然后我删除了端口并再次尝试。现在,我已成功获得服务器的响应:

HttpPost httpPost = new HttpPost("https://xyz.com/user?username=abc&password=123");
.. . .. . .
httpClient.execute(httpPost);
..  .. . . . . 

工作正常,但我还有一个问题: 为什么我在删除端口后成功响应?

答案 1 :(得分:0)

前几天我遇到了同样的问题,我通过移除443端口解决了这个问题。 我仍然不明白为什么会造成问题。问题是,如果我将网址复制/粘贴到浏览器,它就可以正常工作......