Search Unity

UnityWebRequest Cookies

Discussion in 'Scripting' started by Picky-Salamander, Feb 2, 2016.

  1. Picky-Salamander

    Picky-Salamander

    Joined:
    Apr 26, 2013
    Posts:
    27
    I'm using Unity 5.3.1f1 and I'm having some issues setting cookies in UnityWebRequest. I'm getting the following error:

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

    I'm trying to do this while targeting iOS in the editor (I've seen this on all other targets we use as well). Is there something I'm doing wrong? Here is sample code of what I'm doing:

    Code (CSharp):
    1. UnityWebRequest testrequest = new UnityWebRequest("http://test.mydomain.com");
    2. testrequest.SetRequestHeader("Cookie", string.Format("session={0}", token));
    The error occurs on line two of this sample.

    Thanks for the help!
     
    brave_mike likes this.
  2. MarioRuiz

    MarioRuiz

    Joined:
    Nov 7, 2009
    Posts:
    161
  3. SlyRipper

    SlyRipper

    Joined:
    Jun 19, 2012
    Posts:
    251
    The UnityWebRequest class is still pretty new and experimental as mentioned in some posts/changelogs.. so there still might be errors and not implemented features for now..

    Maybe you can use the old WWW class for it, as long as WebRequest was not fixed/updated to work properly?
    Just found this link, maybe it helps at least to use the cookies for now:
    http://answers.unity3d.com/questions/255051/www-and-cookieslogin.html
     
    Last edited: Feb 17, 2016
  4. Picky-Salamander

    Picky-Salamander

    Joined:
    Apr 26, 2013
    Posts:
    27
    Has anyone gotten this to work? I'd really like to start using UnityWebRequest, but this unfortunately is a blocker for us (Our security structure requires using cookies).
     
  5. CADS

    CADS

    Joined:
    Jun 20, 2013
    Posts:
    13
  6. kromenak

    kromenak

    Joined:
    Feb 9, 2011
    Posts:
    259
    Hey, this is very hacky, but the issue seems to be that the call to UnityWebRequest.SetRequestHeader incorrectly flags "Cookie" as an illegal header field name on some platforms. As such, since I really just need a fix that works in development only (not production) until 5.4 is released in non-beta form, I was able to do this, which seems to work OK:

    Code (CSharp):
    1. MethodInfo dynMethod = unityWebRequest.GetType().GetMethod("InternalSetRequestHeader", BindingFlags.NonPublic | BindingFlags.Instance);
    2. dynMethod.Invoke(unityWebRequest, new object[] { "Cookie", "blahblahblah" });
    It requires using System.Reflection for the MethodInfo stuff.

    Perhaps this would work fine in production too, but haha - it seems a bit sketchy, and I don't know if it circumvents any under-the-hood stuff that could cause bugs or other problems.

    So, while I appreciate that it will be fixed in 5.4, it's been two months, aaaaand I need stuff to work now :).
     
    MV10 likes this.
  7. v0kb0l0k

    v0kb0l0k

    Joined:
    Feb 21, 2013
    Posts:
    9
    Version 5.4.0b1 throws same in WebGL

    Code (CSharp):
    1. SetRequestHeader("Cookie", "ClientVersion=1.0.0; Aspect=Aspect_16_x_9; Role=; Os=win");
     
    Last edited: Jun 15, 2016
  8. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    867
    I'm having the sasme problem, any news?
     
  9. Picky-Salamander

    Picky-Salamander

    Joined:
    Apr 26, 2013
    Posts:
    27
    What version did you try it on (I haven't upgraded to 5.4 yet)?
     
  10. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    867
    The latest one 5.4.0f3.
     
  11. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    867
  12. Picky-Salamander

    Picky-Salamander

    Joined:
    Apr 26, 2013
    Posts:
    27
    Can anyone confirm if this is fixed in non-WebGL platforms?

    WebGL isn't so much of a problem for us, as it uses the browser's session cookies that already exist. In other platforms though, I have to set the session, CSRF, etc cookies manually. We had to write a special security routine in our platform just for Unity so that we can work around this and WWW's faults. It would be great if we could just use a normal cookie way of doing it.
     
unityunity