Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java HttpURLConnection源代码getHeaderField是否始终返回null?_Java_Http - Fatal编程技术网

Java HttpURLConnection源代码getHeaderField是否始终返回null?

Java HttpURLConnection源代码getHeaderField是否始终返回null?,java,http,Java,Http,我试图研究HttpURLConnectionJava源代码,以了解它是如何基于套接字编程实现Http连接的,但我遇到了如下问题: /** * Returns the value for the <code>n</code><sup>th</sup> header field. * Some implementations may treat the <code>0</code><sup>th</sup&

我试图研究
HttpURLConnection
Java源代码,以了解它是如何基于套接字编程实现Http连接的,但我遇到了如下问题:

/**
 * 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;
}


getResponseCode()
方法中,有一段代码如下:

    /*
     * 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;
/*
*如果无法创建状态行,则重新引发任何异常
*getInputStream抛出了。
*/
字符串statusLine=getHeaderField(0);
if(statusLine==null){
如果(exc!=null){
if(exc instanceof RuntimeException)
抛出(运行时异常)exc;
其他的
抛出(IOException)exc;
}
返回-1;
}
/*
*检查状态行-应按照
*RFC 2616第6.1节:-
*
*状态行=HTTP版本SP状态代码SP原因短语
*
*如果无法解析状态行,则返回-1。
*/
if(statusLine.startsWith(“HTTP/1”)){
int codePos=statusLine.indexOf(“”);
如果(代码位置>0){
int phrasePos=statusLine.indexOf(“”,codePos+1);
if(phrasePos>0&&phrasePos


为什么
getHeaderField(int n)
总是返回null??要点是什么??我应该如何解释这两种方法?

虽然没有说明,但HttpURLConnection被视为一个抽象基类(实际上并不抽象)。当您调用
URL#openConnection()
时,实际上会得到编译器特定类的实例;对于Sun/Oracle编译器,它是。您会发现其中的
getHeaderField
的实现更有意义