HTTP “date” header is missing in UnityWebRequest

◇◆丶佛笑我妖孽 提交于 2019-12-12 03:49:05

问题


I am working with Unity3d and am trying to send a GET request to an API that requires the Date request header.

However UnityWebRequest does not send it automatically (I have checked with https://requestb.in/) and I cannot set it manually since "date" is one of the protected headers.

UnityWebRequest www = UnityWebRequest.Get (fullURL);

www.SetRequestHeader("Date", dateTimeString);

yield return www.Send();

and I get the following error:

ArgumentException: Cannot set Request Header Date - name contains illegal characters or is not user-overridable

I am able to communicate with the API successfully if I use the WWW class (which does allow me to set the "date" header manually) but am trying to do the same with the UnityWebRequest since WWW will be deprecated soon.

I tried to use System.Reflection's "InternalSetRequestHeader" (as implemented in https://forum.unity3d.com/threads/unitywebrequest-cookies.383530/#post-2621262) as follows:

System.Reflection.MethodInfo dynMethod = myReq.GetType ().GetMethod ("InternalSetRequestHeader", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);

dynMethod.Invoke (myReq, new object[]{"Date", dateTimeString });

but then I get the following error:

InvalidOperationException: Cannot override system-specified headers

So, how can I make sure that the "date" request header is sent?

Is there a solution or do I have to use an object from a different library such as .NET's HttpWebRequest?


回答1:


With UnityWebRequest you can't set custom values fro Date field, check docs.

Also, check RFC 2616, section 4.2, in particular RFC 2616, section 14.18 about Date header. Also, check RFC 2616 section 3.3.1.

Are you sure your dateTimeString is in correct format?



来源:https://stackoverflow.com/questions/40032082/http-date-header-is-missing-in-unitywebrequest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!