问题描述
我正在使用Unity3d,我正在尝试向需要Date请求标头的API发送GET请求。
I am working with Unity3d and am trying to send a GET request to an API that requires the Date request header.
但UnityWebRequest不会自动发送它(I已经使用)进行了检查,我无法手动设置,因为date是其中之一受保护的标题。
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();
我收到以下错误:
I如果我使用WWW类(它允许我手动设置日期标题),我能够成功地与API通信,但我正在尝试使用UnityWebRequest,因为WWW将很快被弃用。
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.
我试图使用System.Reflection的InternalSetRequestHeader(在)如下:
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 });
然后我收到以下错误:
InvalidOperationException:无法覆盖系统指定的标题
那么,我怎样才能确保 date发送请求标头?
So, how can I make sure that the "date" request header is sent?
是否有解决方案或者我是否必须使用来自不同库的对象,例如.NET的HttpWebRequest?
Is there a solution or do I have to use an object from a different library such as .NET's HttpWebRequest?
推荐答案
使用UnityWebRequest,您无法为日期
字段设置自定义值,请查看。
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.
您确定 dateTimeString
的格式是否正确?
Are you sure your dateTimeString
is in correct format?
这篇关于HTTP“日期” UnityWebRequest中缺少标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!