Unity3D-将自定义标头添加到WWWForm [英] Unity3D - Add custom headers to WWWForm

查看:1221
本文介绍了Unity3D-将自定义标头添加到WWWForm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我运行的C#代码:

Here is the C# Code I ran:

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

如上所示,我尝试将AuthorizationHost字段添加到标题中,然后为了确保分配其值.但是Unity3D每次都会在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. However Unity3D throws a Error on formData.headers ["Authorization"] everytime.

以下是错误消息:

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)

推荐答案

WWWForm.headers 变量是只读的.当您调用Add函数时,它实际上并没有添加任何内容.这就是为什么出现此错误的原因,因为数据没有添加到 WWWForm.headers .

The WWWForm.headers variable is read only. When you call the Add function, it's not really adding anything. That's why you are getting that error because the data is not added to the WWWForm.headers.

Unity的WWW类最近更改了.要添加标题,您必须创建字典,然后将Dictionary传递给WWW构造函数的第三个参数.

Unity's WWW class changed recently. To add headers, you have to create Dictionary then pass that Dictionary to the third parameter of the WWW constructor.

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

类似这样的东西:

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


如果要发布表单,则可以使用WWWFormDictionary的组合来进行.只需使用 WWWForm.data WWWForm转换为数组,然后将其传递给WWW构造函数的第二个参数.


If you have form to post, you can use a combination of WWWForm and Dictionary to do that. Just convert the WWWForm to array with WWWForm.data then pass it to the second parameter of the WWW constructor.

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

这篇关于Unity3D-将自定义标头添加到WWWForm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆