Linked Questions

Popular Questions

Java HTTPClient giving connection refused exception

Asked by At

I am trying to create a rest client which posts an xml string and retreives an xml response.But when i try it using the below code ,i am getting the issue connection refused.Also when i hit my mock url using postman addon in chrome i am getting the xml response.Kindly help me out in figuring out why the issue is occuring and how i should be solving this

My Code

public String testMockWS(String inputString) throws KeyManagementException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException{
    //String inputStr =  "";

    String postURL = "https://demo0667044.mockable.io/testXMLRestWS";

    HttpClient client = new DefaultHttpClient();
    HttpPost postRequest = new HttpPost(postURL);

    try{
        StringEntity input = new StringEntity(inputString);
        input.setContentType("text/xml");
        postRequest.setEntity(input);   
        CloseableHttpResponse response = client.execute(postRequest);
        if(response.getStatusLine().getStatusCode() != 201){
            throw new RuntimeException("Failed : HTTP error code : "
                    + response.getStatusLine().getStatusCode());
        }
        BufferedReader br = new BufferedReader(
                new InputStreamReader((response.getEntity().getContent())));

        String output;
        System.out.println("Output from Server .... \n");
        while ((output = br.readLine()) != null) {
            System.out.println(output);
        }

        client.getConnectionManager().shutdown();

    } catch(Exception ex){

        ex.printStackTrace();
    } 

    return null;
}

in which input string is the xml input i want to post.The error i am getting is given below :

Error

2019-02-07 10:23:01 DEBUG BasicClientConnectionManager:158 - Get connection for route {}->http://demo0667044.mockable.io
2019-02-07 10:23:01 DEBUG DefaultClientConnectionOperator:174 - Connecting to demo0667044.mockable.io:80
2019-02-07 10:23:02 DEBUG DefaultClientConnection:176 - Connection [email protected] closed
2019-02-07 10:23:02 DEBUG DefaultClientConnection:160 - Connection [email protected] shut down
2019-02-07 10:23:02 DEBUG BasicClientConnectionManager:196 - Releasing connection [email protected]
java.net.ConnectException: Connection refused: connectOutput : 
 null

    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:117)
    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:177)
    at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:304)
    at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:611)
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:446)
    at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:863)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57)
    at com.util.TestJavaFile.testMockWS(TestJavaFile.java:128)
    at com.util.TestJavaFile.main(TestJavaFile.java:57)

Kindly help me resolve this issue and get the response.I have also tried putting accept trust strategy thinking it might be SSL issue .But that also did not work out.Kindly help me out getting response back.

Related Questions