Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

WWW exception in 2017.1 but not 5.6

Discussion in '2017.1 Beta' started by peteradvr, Jun 14, 2017.

  1. peteradvr

    peteradvr

    Joined:
    Dec 22, 2016
    Posts:
    11
    The following code used to work great in 5.5 and 5.6 but is now throwing an exception in 2017.1. I'm trying to figure out if I'm just fundamentally doing something wrong that happened to work in 5.6 but now fails in 2017.1 or if this is a Unity bug.

    private static readonly string contentType = "Content-Type";
    private static readonly string contentLength = "Content-Length";

    internal static WWW PostJsonToURL(string uploadText, string postURL)
    {
    System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
    Dictionary<string, string> postHeader = new Dictionary<string, string>();

    postHeader.Add(contentType, "text/json");
    postHeader.Add(contentLength, uploadText.Length.ToString());

    WWW request = new WWW(postURL, encoding.GetBytes(uploadText), postHeader);
    return request;
    }

    when I run this in 2017.1 I get this exception in the WWW constructor:

    InvalidOperationException: Cannot override system-specified headers
    UnityEngine.Networking.UnityWebRequest.SetRequestHeader (System.String name, System.String value) (at C:/buildslave/unity/build/artifacts/generated/common/modules/UnityWebRequest/WebRequestBindings.gen.cs:471)
    UnityEngine.WWW..ctor (System.String url, System.Byte[] postData, System.Collections.Generic.Dictionary`2 headers) (at C:/buildslave/unity/build/Runtime/WebRequestWWW/UWRWWW.cs:62)

    Any advice?
     
  2. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    My guess is that it's not allowing you to override the Content-Length header. It's probably setting that automatically internally (as it really should be anyway).
     
  3. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,731
    In 2017.1 WWW class was rewritten to be a wrapper on top of UnityWebRequest. This imposes a more strict rules for headers you can set, specifically not allowing to set headers that are either set automatically of can cause problems.
    The Content-Type is set automatically to correct value and should not be set manually. Even in old WWW implementation setting this header was asking for trouble.
     
    ShahSoft and Dustin-Horne like this.
  4. peteradvr

    peteradvr

    Joined:
    Dec 22, 2016
    Posts:
    11
    Thanks for the help. That worked. Removing Content-Length fixed my issue.
     
    ShahSoft, guetta18 and Dustin-Horne like this.