簡體   English   中英

Android:URLConnection的setRequestProperty函數,此方法做什么?

[英]Android: setRequestProperty function of URLConnection, what does this method do?

盡管我知道StackOverflow已經具有HttpURLConnection問題的setRequestProperty函數 ,但我真的不明白此方法的作用。 調試時無法“進入” (F7)。 我按Ctrl-B可以查看方法的主體,但是它只有以下代碼:

public void setRequestProperty(String field, String newValue) {
        checkNotConnected();
        if (field == null) {
            throw new NullPointerException("field == null");
        }
    }

checkNotConnected

private void checkNotConnected() {
        if (connected) {
            throw new IllegalStateException("Already connected");
        }
    }

我的意思是將值放入字段的代碼在哪里? 任何解釋表示贊賞。

更新(2015/08/08):我找到了查看其實現的方法。 由於是抽象的,因此必須使用Ctrl-Alt-B而不是Ctrl-B進行查看。

我想您訪問的方式有誤,或者有某種受保護的代碼...

原始功能是這樣的:

/**
 * Sets the general request property. If a property with the key already
 * exists, overwrite its value with the new value.
 * ...
 */
public void setRequestProperty(String key, String value) {
    if (connected)
        throw new IllegalStateException("Already connected");
    if (key == null)
        throw new NullPointerException ("key is null");

    if (requests == null)
        requests = new MessageHeader();

    requests.set(key, value);
}

在哪里requests.set(key, value)做你要的:)!

這是setRequestProperty()的源代碼

設置常規請求屬性。 如果具有鍵的屬性已經存在,則用新值覆蓋其值。

注意:HTTP要求所有請求屬性都可以合法地具有多個具有相同鍵的實例,才能使用逗號分隔的列表語法,該語法允許將多個屬性附加到單個屬性中。

參數:

鍵入通過其知道請求的關鍵字(例如“ accept”)。 重視與之相關的價值。

拋出: java.lang.IllegalStateException

如果已經連接java.lang.NullPointerException

如果key為null,請參見:getRequestProperty(java.lang.String)

 public void setRequestProperty(String key, String value) {
    if (connected)
       throw new IllegalStateException("Already connected");
   if (key == null)
        throw new NullPointerException ("key is null");

    if (requests == null)
        requests = new MessageHeader();

   requests.set(key, value);
   }

源鏈接

我找到了查看其實施方式的方法。 由於是抽象的,因此必須使用Ctrl-Alt-B而不是Ctrl-B進行查看。

查看抽象方法的實現

setRequestProperty的后台源代碼如下(在jre \\ lib \\ rt.jar內部):

public void setRequestProperty(String var1, String var2) {
        if(this.connected) {
            throw new IllegalStateException("Already connected");
        } else if(var1 == null) {
            throw new NullPointerException("key is null");
        } else {
            if(this.isExternalMessageHeaderAllowed(var1, var2)) {
                this.requests.set(var1, var2);
            } 
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM