Sam YC
Sam YC

Reputation: 11617

HttpURLConnection source code getHeaderField always return null?

I try to look into HttpURLConnection Java source to get some understandings of how it implements Http connection based on socket programming, but I encounter the problem as below:

/**
 * Returns the value for the <code>n</code><sup>th</sup> header field.
 * Some implementations may treat the <code>0</code><sup>th</sup>
 * header field as special, i.e. as the status line returned by the HTTP
 * server.
 * <p>
 * This method can be used in conjunction with the
 * {@link #getHeaderFieldKey getHeaderFieldKey} method to iterate through all
 * the headers in the message.
 *
 * @param   n   an index, where n>=0.
 * @return  the value of the <code>n</code><sup>th</sup> header field,
 *          or <code>null</code> if the value does not exist.
 * @see     java.net.HttpURLConnection#getHeaderFieldKey(int)
 */
public String getHeaderField(int n) {
    return null;
}



In the getResponseCode() method, there is a piece of code as below:

    /*
     * If we can't a status-line then re-throw any exception
     * that getInputStream threw.
     */
    String statusLine = getHeaderField(0);
    if (statusLine == null) {
        if (exc != null) {
            if (exc instanceof RuntimeException)
                throw (RuntimeException)exc;
            else
                throw (IOException)exc;
        }
        return -1;
    }

     /*
     * Examine the status-line - should be formatted as per
     * section 6.1 of RFC 2616 :-
     *
     * Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase
     *
     * If status line can't be parsed return -1.
     */
    if (statusLine.startsWith("HTTP/1.")) {
        int codePos = statusLine.indexOf(' ');
        if (codePos > 0) {

            int phrasePos = statusLine.indexOf(' ', codePos+1);
            if (phrasePos > 0 && phrasePos < statusLine.length()) {
                responseMessage = statusLine.substring(phrasePos+1);
            }

            // deviation from RFC 2616 - don't reject status line
            // if SP Reason-Phrase is not included.
            if (phrasePos < 0)
                phrasePos = statusLine.length();

            try {
                responseCode = Integer.parseInt
                        (statusLine.substring(codePos+1, phrasePos));
                return responseCode;
            } catch (NumberFormatException e) { }
        }
    }
    return -1;



How come getHeaderField(int n) always return null?? What are the points?? How should I interpret these two method?

Upvotes: 1

Views: 1930

Answers (1)

Paul Hicks
Paul Hicks

Reputation: 13999

Though it doesn't say it, HttpURLConnection is treated as an abstract base class (without actually being abstract). When you call URL#openConnection(), you actually get an instance of a compiler-specific class; for the Sun/Oracle compiler, it's sun.net.www.protocol.http.HttpURLConnection. You'll find that the implementation of getHeaderField in there makes more sense.

Upvotes: 3

Related Questions