0
這裏是我跑的C#代碼:Unity3D - 添加自定義標題,以WWWForm
WWWForm formData = new WWWForm();
//Adding
formData.headers.Add ("Authorization", "Basic " + System.Convert.ToBase64String(Encoding.UTF8.GetBytes(CONSUMER_KEY + ":" + CONSUMER_SECRET)));
formData.headers.Add ("Host", "api.twitter.com");
//Assigning
formData.headers ["Host"] = "api.twitter.com";
formData.headers ["Authorization"] = "Basic " + System.Convert.ToBase64String (Encoding.UTF8.GetBytes (CONSUMER_KEY + ":" + CONSUMER_SECRET));
Debug.Log (formData.headers ["Authorization"]);
如上圖所示,我試圖Authorization
和Host
字段添加到頭部,然後就指定其值爲了確定。但Unity3D每次都在formData.headers ["Authorization"]
上發生錯誤。
這裏是錯誤消息:
KeyNotFoundException: The given key was not present in the dictionary.
System.Collections.Generic.Dictionary`2[System.String,System.String].get_Item (System.String key) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:150)
Information+Twitter.GetToken() (at Assets/Static Libraries/Information.cs:143)
Information.Initialize() (at Assets/Static Libraries/Information.cs:18)
WorldScript.Awake() (at Assets/WorldScript.cs:16)
奇怪的是,改變一個只讀對象不會給我一個錯誤或警告。 –
我知道它是有線的。每次訪問WWWForm.headers時,您都會得到一個新的Dictionary /副本,或者Unity有一個代碼可以從「Dictionary」中刪除任何內容。除非我有源代碼,否則我無法分辨究竟發生了什麼。 – Programmer