Java连接重置异常,但与邮递员一起工作

时间:2018-04-14 00:43:17

标签: apache-commons-httpclient connection-reset

我突然开始使用api进行连接重置异常,我已经与他沟通了4年。服务器提供商告诉我他没有改变任何东西,我也没有。奇怪的是,当我在邮递员上打电话给同一个api时,我得到了成功的回应。我甚至尝试过python来调用它,但是我得到了同样的错误。

http客户端调用api

HttpClient client = new HttpClient();


    client.getHttpConnectionManager().
    getParams().setConnectionTimeout(5000);
    client.getHttpConnectionManager().
    getParams().setSoTimeout(60000);

    GetMethod method = new GetMethod(smsUrl);

    method.setQueryString(new NameValuePair[] { 
            new NameValuePair("username", user), 
            new NameValuePair("password", pass), 
            new NameValuePair("action", "sendsms"), 
            new NameValuePair("from", "Sender"),
            new NameValuePair("to", toMobile), 
            new NameValuePair("text", textBody)
    });

    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, 
            new DefaultHttpMethodRetryHandler(3, false));

    try {
        int statusCode = client.executeMethod(method);

        if (statusCode != HttpStatus.SC_OK) {
            System.err.println("Method failed: " + method.getStatusLine());
            String responseBody = method.getResponseBodyAsString();
            System.out.println(new String(responseBody));

        }



    } catch (HttpException e) {
        System.err.println("Fatal protocol violation: " + e.getMessage());
        e.printStackTrace();

    } catch (IOException e) {
        System.err.println("Fatal transport error: " + e.getMessage());

        e.printStackTrace();
    } finally {
        // Release the connection.
        method.releaseConnection();
    }  

错误堆栈跟踪

Fatal transport error: Connection reset
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:196)
at java.net.SocketInputStream.read(SocketInputStream.java:122)
at sun.security.ssl.InputRecord.readFully(InputRecord.java:442)
at sun.security.ssl.InputRecord.read(InputRecord.java:480)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:934)
at    sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1332)
at sun.security.ssl.SSLSocketImpl.writeRecord(SSLSocketImpl.java:709)
at sun.security.ssl.AppOutputStream.write(AppOutputStream.java:122)
at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82)
at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:140)
at org.apache.commons.httpclient.HttpConnection.flushRequestOutputStream(HttpConnection.java:827)
at org.apache.commons.httpclient.HttpMethodBase.writeRequest(HttpMethodBase.java:1975)
at org.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.java:993)
at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:397)
at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:170)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:396)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:324)

1 个答案:

答案 0 :(得分:1)

我也遇到过类似的问题。但是在将客户端JDK从7升级到8时解决了。 Ref link