InvalidOperationException:无法覆盖系统指定的标头

时间:2017-10-13 18:33:15

标签: c# rest unity3d

我正在尝试通过Unity制作多人游戏。我使用示例资产尝试,我得到一个错误,如下所示:

InvalidOperationException: Cannot override system-specified headers
UnityEngine.Networking.UnityWebRequest.SetRequestHeader (System.String name, System.String value) (at C:/buildslave/unity/build/artifacts/generated/common/modules/UnityWebRequest/WebRequestBindings.gen.cs:482)
UnityEngine.WWW..ctor (System.String url, System.Byte[] postData, System.Collections.Generic.Dictionary`2 headers) (at C:/buildslave/unity/build/Runtime/WebRequestWWW/UWRWWW.cs:62)
QuizMaker.Administrator.AdminAPI+<api_call>c__Iterator0`1[QuizMaker.Administrator.CheckConnectionResponse].MoveNext () (at Assets/QuizMaker/Scripts/Administrator/AdminAPI.cs:77)
UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
QuizMaker.Administrator.AdminAPI:CheckConnection(Callback`1) (at Assets/QuizMaker/Scripts/Administrator/AdminAPI.cs:227)
QuizMaker.Administrator.ServerSettingsUI:_updateServerStatus() (at Assets/QuizMaker/Scripts/Administrator/Actions/ServerSettingsUI.cs:147)
QuizMaker.Administrator.ServerSettingsUI:UpdateServerStatus() (at Assets/QuizMaker/Scripts/Administrator/Actions/ServerSettingsUI.cs:139)
QuizMaker.Administrator.ServerSettingsUI:Start() (at Assets/QuizMaker/Scripts/Administrator/Actions/ServerSettingsUI.cs:44)

我的代码:

 // create a form for a post data
                WWWForm form = new WWWForm();

                // create a log string
                var logString = string.Format("[API_REQ {0}]", action);

                // add key val if data is empty
                if (data.Length == 0)
                {
                    data = new string[] { "key", "val" };
                }

                // add data from an data array to the form
                for (int i = 0; i < data.Length - 1; i += 2)
                {
                    // add key and value
                    form.AddField(data[i], data[i + 1]);

                    // add log
                    logString += string.Format(" [{0}: {1}]", data[i], data[i + 1]);
                }

                // it needs to be more secure
                var date = DateTime.Now.ToString();
                var headers = form.headers;
                headers["Date"] = date;
                headers["Order"] = Util.generateOrderString(data);
                form.AddField("hash", Util.generateHash(data, date));

                // print log string
                print(logString);

                // create www request
                var www = new WWW(AppConfig.Instance.serverUrl + action, form.data, headers);

                // wait for a response
                yield return www;

                // print log for each api call
                print(string.Format("[API_RES {0}] [{1}]", action, www.text));

                // parse a response
                parseResponse(
                    string.IsNullOrEmpty(www.error),
                    www.text,
                    callback
                    );

当我删除日期变量时,错误修复但是在这段时间我无法连接到服务器。我的连接方法是REST API。提前谢谢。

1 个答案:

答案 0 :(得分:2)

我发现了错误。如果有人遇到同样的问题,只需更改以下变量:

headers["Date"] = date;

headers["date"] = date;

这解决了我的问题,节省了我的时间。最好的问候。