2

I'm making a POST request using Java 8 like this:

URL url = new URL("http://target.server.com/doIt");
URLConnection connection = url.openConnection();
HttpURLConnection httpConn = (HttpURLConnection) connection;

byte[] soapBytes = soapRequest.getBytes();
httpConn.setRequestProperty("Host", "target.host.com");
httpConn.setRequestProperty("Content-Length", soapBytes.length+"");
httpConn.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");
httpConn.setRequestMethod("POST");
httpConn.setConnectTimeout(5000);
httpConn.setReadTimeout(35000);
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
OutputStream out = httpConn.getOutputStream();
out.write(soapBytes);
out.close();

int statusCode;
try {
    statusCode = httpConn.getResponseCode();
} catch (IOException e) {
    InputStream stream = httpConn.getErrorStream();
    if (stream == null) {
        throw e;
    } else {
        // this never happens
    }
}

My soap request contains a document ID and the target server (which hosts a third-party service that I do not own or have access to) returns a PDF document that matches the supplied ID.

Most of the time, the server returns a PDF doc and occasionally the status code is 500 when the document is not available. However, sometimes the call to getResponseCode() throws an IOException with "Invalid Http response".

I thought that a server would always have some response code to return, no matter what happens.

  • Does this mean that server is returning complete garbage that doesn't match the expected format of a HTTP response?
  • Is there a way to get any more information about the actual response?
  • Is there a way to retrieve the raw textual response (if any)?
3
  • From the doc of getResponseCode : IOException : if an error occurred connecting to the server. so I would guess there is some issues about the connection. PS : -1 would be return if the code was not recognize Commented Feb 3, 2017 at 11:52
  • But if that's true, wouldn't the exception be thrown when writing the soap request, which happens before the call to get the response code? Commented Feb 3, 2017 at 11:54
  • 1
    This seems to be a mistake in the javadoc. If you open the source code, you will see it could occurs will opening the input stream ( you can check using the debugger if you can reproduce this easily). Then you can see why (or at least the reason) Commented Feb 3, 2017 at 12:09

1 Answer 1

2

As AxelH points out, there must be something wrong when connecting with the remote server, and in this case you just can't get a valid response.

If you are in a testing environment, you can monitorize the connection at TCP level (not at HTTP level): Put a monitor between your client and the remote server which monitorizes all the TCP traffic exchanged between the two peers. If you are using Eclipse, you can create a TCP monitor.

Sign up to request clarification or add additional context in comments.

4 Comments

Unfortunately, this is very difficult for me to reproduce. I'll see if the service owners can shed any light on the situation. It does seem likely that the connection is being refused, although I would have thought the error message from the IOException would have been "Connection refused".
@RTF Indeed, when you try to connect to a server that is unavailable, you shoud get a "Connection refused" exception. But if, once the connection is stablished, it is abnormally aborted by the remote peer, a different error message is thrown.
Do you know at what point exactly the connection to the remote server is established in my code snippet?
@RTF Sure: Just when you call httpConn.getOutputStream() the connection is implicitly established. Read about this multistep process in URLConnection.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.