鍍金池/ 問答/Java  HTML/ getHeaderField為何總是return null

getHeaderField為何總是return null

各位大佬,我無意中查看源碼的時(shí)候產(chǎn)生如下疑問:
java.net.URLConnection類中有一個(gè)getHeaderFieldDate方法,如下:

@SuppressWarnings("deprecation")
public long getHeaderFieldDate(String name, long Default) {
    String value = getHeaderField(name);
    try {
        return Date.parse(value);
    } catch (Exception e) { }
    return Default;
}

該方法中調(diào)用了一個(gè)getHeaderField方法,如下:

public String getHeaderField(String name) {
    return null;
}

getHeaderField方法總是返回null,這是為什么呢?謝謝!

回答
編輯回答
奧特蛋
    /**
     * Returns the value of the named header field.
     * <p>
     * If called on a connection that sets the same header multiple times
     * with possibly different values, only the last value is returned.
     *
     *
     * @param   name   the name of a header field.
     * @return  the value of the named header field, or {@code null}
     *          if there is no such field in the header.
     */
    public String getHeaderField(String name) {
        return null;
    }

事實(shí)上這個(gè)方法在常見實(shí)現(xiàn)類里都有被覆寫:

clipboard.png

比如常見的 HttpURLConnection 中:

public String getHeaderField(String var1) {
    try {
        this.getInputStream();
    } catch (IOException var3) {
        ;
    }

    return this.cachedHeaders != null 
        ? this.filterHeaderField(var1, this.cachedHeaders.findValue(var1)) 
        : this.filterHeaderField(var1, this.responses.findValue(var1));
}
2017年7月21日 16:40