Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by ESantos · Oct 13, 2016 at 07:49 PM · wwwhttpwebrequestheaders

Date request header is missing from http request(UnityWebrequest)

I 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.

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 eventually.

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?

Thanks

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Oct 13, 2016 at 08:10 PM 0
Share

Uhm, you should have included the code that you're using and tell us for whivh platform you build for. I was never in need to set this header, however i can confirm (by looking at the UnityWebRequest source code) that "date" is on the list of forbidden header keys. Depending on your platform you might be able to use reflection to directly use "InternalSetRequestHeader" to bypass all checks. This of course is a bit dangerous.

Sometimes i'm not sure what UT though when they made certain decisions ^^. In some environments it makes sense to restrict certain headers to avoid abuse, but for example in standalone builds those restrictions make not much sense to me.

Though the UnityWebRequest is still in the "Experimental" namespace AFAI$$anonymous$$.

avatar image ESantos Bunny83 · Oct 13, 2016 at 08:55 PM 0
Share

The relevant bit of the code is the following:

 UnityWebRequest www = UnityWebRequest.Get (fullURL);
 
  www.SetRequestHeader("Date", dateTimeString);
 
  yield return www.Send();

And the error message I get is: ArgumentException: Cannot set Request Header Date - name contains illegal characters or is not user-overridable

I am building to IOS and Android.

You are right about the protected list but not only can I not change it (since it is protected) but Unity does not send the "date" header at all. Other protected headers fields such as "host" and "accept-encoding" cannot be modified either but at least they are sent along with the request.

I have tried to use System.Reflection as you suggested, as follows:

 System.Reflection.$$anonymous$$ethodInfo dyn$$anonymous$$ethod = myReq.GetType ().Get$$anonymous$$ethod ("InternalSetRequestHeader", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
     
  dyn$$anonymous$$ethod.Invoke (myReq, new object[]{"Date", dateTimeString });

but I get the following error:

InvalidOperationException: Cannot override system-specified headers

avatar image Bunny83 ESantos · Oct 14, 2016 at 01:43 AM 1
Share

Too bad. Well in this case they also implemented another check on the native side. So that simply renders the UnityWebRequest class useless for your case, end of story ^^. You might want to use the .NET classes ins$$anonymous$$d.

The HttpWebRequest is not that difficult to use, the only problem is that it's a blocking request, so you would need to use a thread and implement some mechanics to get the result back into the main thread once finished. If you only need a few requests you can wrap all this in a seperate class that is derived from CustomYieldInstruction. That way you can still use it in a coroutine.

If you need to do a lot requests frequently you might want to use a threadpool or a producer - consumer pattern with a permanent worker thread.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by josefgrunig · Feb 20, 2018 at 03:54 PM

There are multiple issues here. First UnityWebRequest is not sending the "Date" header and secondly you are not allowed (since Unity 2017.1) to set it manually anymore. I found a workaround setting the lower case "date" header instead of "Date". Its terrible I know, but there are service like Vuforia Cloud API that require the "date" header and must be set from client side because used in the signature: https://library.vuforia.com/articles/Training/Using-the-VWS-API

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I add a header and a file at the web request at the same time? 0 Answers

Posting raw XML data to web - no parameter name 2 Answers

WWW Windows Phone 8 / Windows RT 8.1 missing first line http status header 0 Answers

Is System.Net.HttpWebRequest supported on Android/iOS 1 Answer

Is HttpWebRequest supposed to work in Web Builds or not? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges