HTTP “date” header is missing in UnityWebRequest

2019-09-11 02:16发布

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条回答
\"骚年 ilove
2楼-- · 2019-09-11 02:37

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?

查看更多
登录 后发表回答