1

Hi I've started using HttpUrlConnection and I have a question regarding on when the Http Request is actually sent.

I read somewhere that the actual request is sent when getInputStream() is called. However, I've written some test code to play around with this for a POST request:

In this version I'm calling on getResponseCode() before getInputStream()

URL obj = new URL(myUrl);
HttpURLConnection httpclient = (HttpURLConnection) obj.openConnection();
httpclient.setRequestMethod("POST");
**int responseCode = httpclient.getResponseCode();** 
    try { 
        inStream = httpclient.getInputStream(); 
    }
    catch (IOException ie) { 
        inStream = httpclient.getErrorStream(); 
    }
System.out.println("response code = " + responseCode);

I receive a response code of 200. So this got me thinking that the request is not being sent at getInputStream(), but at an earlier method. Does anyone have any insight on this?

Thanks!

2 Answers 2

1

Code Insight for HttpURLConnection#getResponseCode() - openJDK 7

public int getResponseCode() throws IOException {
454        /*
455         * We're got the response code already
456         */
457        if (responseCode != -1) {
458            return responseCode;
459        }
460
461        /*
462         * Ensure that we have connected to the server. Record
463         * exception as we need to re-throw it if there isn't
464         * a status line.
465         */
466        Exception exc = null;
467        try {
468            getInputStream();
469        } catch (Exception e) {
470            exc = e;
471        }
472        ...

Basically response code will -1 at initialization which means we have not got any response code. So It will establish a connection URL#getInputStream() and get the response code.

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

3 Comments

This is indeed the case, reading the source of java.net.HttpURLConnection in JDK8. Afterwards, it reads the status line from the header and obtains the code.
umm... Actually it makes the connection.
So at the very core, it is getInputStream() that will be making the connection/sending the request? Looks like getResponseCode() uses getInputStream to make the connection.
0

getResponseCode() does the the same thing: it flushes the request and starts reading the response.

Actually, you MUST try to start reading the response (code or stream) for the request to be totally sent to the server.

2 Comments

So does getResponseCode() and getInputStream() both flush the request? in my code I commented out getResponseCode(), so I never call on getResponseCode. Yet, I'm still getting a proper response...so is getResponseCode required for the request to be completely sent?
Either one or the other

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.