简体   繁体   English

Unity3D-将自定义标头添加到WWWForm

[英]Unity3D - Add custom headers to WWWForm

Here is the C# Code I ran: 这是我运行的C#代码:

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"]);

As shown above, I tried to add Authorization and Host fields to the header and then Assigning them values just to be sure. 如上所示,我尝试将“ Authorization和“ Host字段添加到标题中,然后为了确保分配其值。 However Unity3D throws a Error on formData.headers ["Authorization"] everytime. 但是,Unity3D每次都会在formData.headers ["Authorization"]上引发错误。

Here is the Error Message: 这是错误消息:

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)

The WWWForm.headers variable is read only. WWWForm.headers变量是只读的。 When you call the Add function, it's not really adding anything. 当您调用Add函数时,它实际上并没有添加任何东西。 That's why you are getting that error because the data is not added to the WWWForm.headers . 这就是为什么出现此错误的原因,因为数据没有添加到WWWForm.headers

Unity's WWW class changed recently. Unity的WWW类最近更改了。 To add headers, you have to create Dictionary then pass that Dictionary to the third parameter of the WWW constructor. 要添加标题,您必须创建Dictionary,然后将该Dictionary传递给WWW构造函数的第三个参数。

public WWW(string url, byte[] postData, Dictionary<string, string> headers);

Something like this: 像这样:

Dictionary<string, string> headers = new Dictionary<string, string>();
headers.Add("User-Agent", "Mozilla / 5.0(Windows NT 10.0; WOW64) AppleWebKit / 537.36(KHTML, like Gecko) Chrome / 55.0.2883.87 Safari / 537.36");

WWW www = new WWW("http://www.thismachine.info/", null, headers);
yield return www;
Debug.Log(www.text);

If you have form to post, you can use a combination of WWWForm and Dictionary to do that. 如果您要发布表单,则可以结合使用WWWFormDictionary来完成。 Just convert the WWWForm to array with WWWForm.data then pass it to the second parameter of the WWW constructor. 只是转换了WWWForm到阵列WWWForm.data然后将它传递给第二个参数WWW构造。

Dictionary<string, string> headers = new Dictionary<string, string>();
headers.Add("User-Agent", "Mozilla / 5.0(Windows NT 10.0; WOW64) AppleWebKit / 537.36(KHTML, like Gecko) Chrome / 55.0.2883.87 Safari / 537.36");

WWWForm formData = new WWWForm();
formData.AddField("UserName", "Programmer");
formData.AddField("Password", "ProgrammerPass");

WWW www = new WWW("http://www.thismachine.info/", formData.data, headers);
yield return www;
Debug.Log(www.text);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM