HttpClient执行方法抛出java.net.SocketException:连接重置

时间:2017-07-05 19:35:29

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

在client.execute(request)方法获取'连接重置'

public HttpResponse createAndSendRequest(Class<? extends HttpRequestBase> httpClass)  {
    HttpClient client = HttpClientBuilder.create().build();
    Constructor<?>[] constructors =  httpClass.getConstructors();
    Constructor<?> ctor = null;
    for(int i = 0; i < constructors.length; ++i) {
        if(constructors[i].getParameterTypes().length == 1 && 
                constructors[i].getParameterTypes()[0].isAssignableFrom(String.class)) {
            ctor = constructors[i];
        }
    }

    HttpRequestBase request = null;        
    if(ctor == null) {
        request = httpClass.newInstance();
        request.setURI(new URI(getUrl()));
    } else {
        request = (HttpRequestBase) ctor.newInstance(getUrl());
    }                          

    create(request);
    return client.execute(request);
    } 



protected <T extends HttpRequestBase> RequestBuilder<?> create(T request)  {               
                String payload = PAYLOAD_DATA; 

                HttpEntity entity = new ByteArrayEntity(payload.getBytes("UTF-8"));

                if(request instanceof HttpEntityEnclosingRequestBase) {
                    ((HttpEntityEnclosingRequestBase)request).setEntity(entity);
                }
            }

发生异常:

Exception Message: Connection reset

StackTrace:

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.startHandshake(SSLSocketImpl.java:1359)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1343)
    at org.apache.http.conn.ssl.SSLConnectionSocketFactory.createLayeredSocket(SSLConnectionSocketFactory.java:396)
    at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:355)
    at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:142)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:359)
    at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:381)
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:237)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:185)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:111)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56)
    at api.restServices.requestBuilder.RequestBuilder.createAndSendRequest(RequestBuilder.java:278)
    at api.restServices.requestBuilder.RequestBuilder.createAndSendRequest(RequestBuilder.java:343)
    at api.restServices.requestBuilder.AssemblyRequestBuilder$AssemblyRequestType.createAndSendRequest(AssemblyRequestBuilder.java:245)
    at api.restServices.RestServices.joinMarket(RestServices.java:400)
    at com.test.App.marketing.ID_1999_Verify_Market_value_and_description_are_displayed_to_MD.test_ID_1999_Verify_Market_value_and_description_are_displayed_to_MD(ID_1999_Verify_Market_value_and_description_are_displayed_to_MD.java:69)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)

0 个答案:

没有答案