Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Trouble with UnityWebRequest WWW

Discussion in 'Editor & General Support' started by Tor7uga, Jan 12, 2018.

  1. Tor7uga

    Tor7uga

    Joined:
    Jan 12, 2018
    Posts:
    2
    I have a program which is runnin on a Hololens.
    It takes a picture when a "tap gesture" is performed, puts it into a byte array and sends that information, along with headers to Microsofts facial recognition Api and shows the results.
    For my colleagues this works fine, but for whatever reason Unity's www module just returns an empty object when i try.

    I know the API should use roughly 4 seconds to give a response. I tested this by using the same URL, with the same input headers with an external program (Postman). My colleagues also report roughly the same response time. We have ensured our settings are the same, both in Unity before building the project, and in Visual Studio. The necessary capabilities are also all turned on for the Hololens.

    So, below are (i think) the important parts of the code.
    imageBufferList2 is a list that contains the captured picture as bytes.
    Code (CSharp):
    1. StartCoroutine(PostToFaceAPI(imageBufferList2.ToArray(), cameraToWorldMatrix, pixelToCameraMatrix));
    Code (CSharp):
    1.  IEnumerator<object> PostToFaceAPI(byte[] imageData, Matrix4x4 cameraToWorldMatrix, Matrix4x4 pixelToCameraMatrix) {
    2.  
    3. string face_key1 = "Personal API key";
    4.  
    5. var headers = new Dictionary<string,string>() {
    6.       { "Ocp-Apim-Subscription-Key", face_key1 },
    7.       { "Content-Type", "application/octet-stream" }
    8.  };
    9.  
    10. string face_api = "https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect";
    11. string uri = face_api + "?returnFaceId=true";
    12.  
    13. var www = new WWW(uri, imageData, headers);
    14.         yield return www;
    15. if(!string.IsNullOrEmpty(www.error)) Debug.Log("Error in WebRequest");
    That last if-sentence will not trigger, so the request seems to think everything is fine. But all of this takes roughly 52 ms instead of 4 seconds, and the www result object is just empty (Not null, but empty).

    I tried to change the url to this, which is a dummy url that does not exist.
    Code (CSharp):
    1.  string face_api = "http://xn--oiashdfoiasdjfhgkjhdfgk-mmc.com/";
    Still www.error is empty, and there are no errors thrown by the program.

    So it seems like the WWW module simply is not triggering, and i have not found any reason why. Any help would be appreciated!
     
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,722
    Are you on Unity 2017.3 ?
    There is an outstanding bug that WWW uses chunked transfer in this release, so your request can fail because of that if server doesn't support chunked transfer.
    Try to replace WWW with UnityWebRequest and set chunkedTransfer property to false.

    A fix for this bug is in the pipeline, should come out in patch releases soon-ish.
     
    Liut2018 and ZackMFleischman like this.
  3. Natzke

    Natzke

    Joined:
    Feb 5, 2017
    Posts:
    7
    So brutal. I've been chasing this bug for HOURS. chunkedTransfer = false solved it! THANK YOU!!
     
    ZackMFleischman likes this.
  4. Tor7uga

    Tor7uga

    Joined:
    Jan 12, 2018
    Posts:
    2
    Using UnityWebRequest solves the issue.
     
    Last edited: Jan 26, 2018
  5. tenh_un

    tenh_un

    Joined:
    Jun 24, 2019
    Posts:
    5
    So brutal. I've been chasing this bug for HOURS. chunkedTransfer = false solved it! THANK YOU

    i do the same but it doesn't work, where do you put chunkedTransfer = false?
     
  6. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,722
    It's a property on UnityWebRequest object. Set it before sending the request.