2

我正在开发一个应用程序来使用 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)
4

2 回答 2

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

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

于 2013-04-22T01:11:20.577 回答
0

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

于 2013-11-01T08:30:23.893 回答