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

unitywebrequest sometimes returns null

Discussion in 'Scripting' started by bobcccc, Jul 14, 2020.

  1. bobcccc

    bobcccc

    Joined:
    Mar 12, 2014
    Posts:
    122
    sometimes the webrequest just returns null and yet when i click the button to do the request again it works fine...this does not happen all the time but enough that it is annoying, is there something wrong with my code??

    Code (CSharp):
    1. IEnumerator webrequestcall()
    2.     {
    3.         WWWForm form = new WWWForm();
    4.         form.AddField("gcid", gcid);
    5.         UnityWebRequest www = UnityWebRequest.Post("url", form);
    6.         yield return www.SendWebRequest();
    7.         if (www.isNetworkError || www.isHttpError)
    8.         {
    9.             Debug.Log("Error contacting Server");
    10.         }
    11.         else
    12.         {
    13.            
    14.             string returndata = www.downloadHandler.text;
    15.             Debug.Log("aaa " + returndata);          
    16.     }
     
    LuckyMoney likes this.
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    This is unlikely your problem, but
    UnityWebRequest
    implements the
    IDisposable
    interface, which means you should probably stick it in a
    using()
    block.

    Also, try other endpoints than the one URL. Also, are you seeing any errors? If you build to device, are you seeing any exceptions such as SSL exceptions?
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,892
    It's also possible that your server doesn't like whatever your "gcid" value is. Do you have server logs for the requests that return null? What was the server's response in those cases?

    Also, presumably "url" is just a string you're using on the forums and not what's actually in your code right?
     
  4. bobcccc

    bobcccc

    Joined:
    Mar 12, 2014
    Posts:
    122
    yeah the addfield gcid and url are just placeholders.. There are no errors in the log at all...it just returns null. it has happened when using multiple urls and as i said does not happen all the time but just some times, really not sure what the problem is and why it only happens sometimes and not all the time.
     
  5. bobcccc

    bobcccc

    Joined:
    Mar 12, 2014
    Posts:
    122
    how would i put this in a using() block, never done that before? will give it a go and see if it works.
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,892
  7. bobcccc

    bobcccc

    Joined:
    Mar 12, 2014
    Posts:
    122
    OK I made the change to the following code

    Code (CSharp):
    1.  using (UnityWebRequest request = UnityWebRequest.Post("url", form))
    2.         {
    3.             yield return request.SendWebRequest();
    4.             if (request.isNetworkError || request.isHttpError)
    5.             {
    6.                 Debug.Log("Error contacting Server");
    7.             }
    8.             else
    9.             {
    10.                 string returndata = request.downloadHandler.text;
    11.                 Debug.Log("aaa " + returndata);
    12.              
    13.             }
    14. }
    The same thing is still happening however what I find very interesting is it only happens if you try and send a request around the 2 second mark of the first one returning...If you click the button to send <2 seconds it works fine..if you wait for about 4 seconds or more it works fine..if you wait approx 2 seconds and send it, it Always comes back blank...
     
  8. HEROTECH70

    HEROTECH70

    Joined:
    May 24, 2017
    Posts:
    74
    Are you calling "IEnumerator webrequestcall()" directly from the button?

    Try calling a method that starts a coroutine with webrequestcall()
     
  9. bobcccc

    bobcccc

    Joined:
    Mar 12, 2014
    Posts:
    122
    button is linked to a function when clicked

    Code (CSharp):
    1. public void buttonclick(){
    2.    startcoroutine(webrequestcall());
    3. }
     
  10. HEROTECH70

    HEROTECH70

    Joined:
    May 24, 2017
    Posts:
    74
    this behaviour is really strange btw, you should try with another entirely different post request that you know is guaranteed to work and see if it behaves the same
     
  11. bobcccc

    bobcccc

    Joined:
    Mar 12, 2014
    Posts:
    122
    It seems to happen with all request, i have made multiple now and they all seem to fail at some time or another, the way i go around it was to duplicate the IEnumerator function and call it webrequestcall2, when webrequest call returns null it immediately starts coroutine for webrequestcall2 and that one works. I gave up on trying to fix the problem, absolutely no idea what is causing it, i have tried calling dispose and using the using() block. All seem to fail maybe 1 in 20 times always around the 2 second mark of the 1st coroutine returning from the server.
     
  12. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,722
    Does it look like request was successful but no data returned?
    If so, setup HTTP debugging proxy (such as Fiddler) and examine the request/response of the failed one.
    If the error is something else, paste the contents from the console.
     
  13. bobcccc

    bobcccc

    Joined:
    Mar 12, 2014
    Posts:
    122
    there is no error in the logs at all, I did play around with the other values to see what was happening, if i try and debug.log the returndata the app just hangs however if you just check it is null then the app continues.

    I also tried logging getresponseheaders and it received 2 headers when the webrequest succeeded but none when the value returned null. It seems almost like unity is cancelling the request before it even reaches the server and just returning a null value.

    I will look into fiddler later on and see if that can show anything.
     
  14. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,722
    What is null?
    The error you describe is a symptom of using UWR after it has been disposed, which would mean an error in your code. Can't tell exactly without knowing what your actual code looks like exactly.
     
  15. bobcccc

    bobcccc

    Joined:
    Mar 12, 2014
    Posts:
    122
    my code is below someone clicks a button and then the function starts the coroutine


    Code (CSharp):
    1.  public void jobclick()
    2.     {
    3.             StartCoroutine(DoStuff());
    4.     }
    5.  
    6. IEnumerator DoStuff()
    7. {
    8.     WWWForm form = new WWWForm();
    9.     form.AddField("Jobtype", joby);
    10.     UnityWebRequest www = UnityWebRequest.Post("urlhere", form);
    11.     yield return www.SendWebRequest();
    12.     if (www.isNetworkError || www.isHttpError) { Debug.Log("Error contacting Server"); }
    13.     else
    14.     {
    15.         string returndata = www.downloadHandler.text;
    16.         if (returndata.Length > 2)
    17.         {
    18.             string[] myarray = returndata.Split('|');
    19.             if (myarray[0] == "1")
    20.             {
    21.                // do stuff here
    22.             }
    23.             else
    24.             {
    25.                 // do error message
    26.             }
    27.         }
    28.         else
    29.         {
    30.             Debug.Log("retyring code");
    31.             StartCoroutine(DoJobs2());  // starts a coroutine to resend the data
    32.         }
    33.     }
    34.     www.Dispose();
    35.  
    36. }
     
    Romaissa_Guetoutou likes this.
  16. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,722
    At what point do you get null? What is the exact error in console?
     
  17. bobcccc

    bobcccc

    Joined:
    Mar 12, 2014
    Posts:
    122
    no error in console at all
    string returndata = www.downloadHandler.text;
    returndata just returns null as in nothing there...i even added a line on the server error_log to log everytime it hits the server and when it returns null it doesn't even reach the server. I will test later on with another url and see if the error persists but it errors so fast that it seems like it doesn't even have time for the data to get to my server before it returns null in www.downloadHandler.text
     
  18. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,892
    Maybe the response just couldn't be encoded as a string. Did you check the raw byte data from download handler?
     
  19. bobcccc

    bobcccc

    Joined:
    Mar 12, 2014
    Posts:
    122
    I didnt but i did output the headers as in

    UnityWebRequest.GetResponseHeaders

    When www.downloadhandler.text returned null the response headers were also null as in nothing in them, when the request worked fine the response headers had a dictionary with 2 strings in them.
     
  20. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,722
    That sounds low. Normally there are much more headers. What is your server setup?
    You certainly need to setup Fiddler or similar and examine the request and response.
     
    Bunny83 likes this.
  21. AnthonySharp

    AnthonySharp

    Joined:
    Apr 11, 2017
    Posts:
    88
    DownloadHandler.data will return null when nothing is received from the server (https://docs.unity3d.com/ScriptReference/Networking.DownloadHandler-data.html). And DownloadHandler.text seems to just be a string version of .data.

    .isDone should technically be true, but maybe make sure.

    Other than that it could be a number of things ... your server is silently failing ... your system is interfering with the network calls ... Unity has some weird bug ... the Chinese government is listening to your post requests ... who knows.
     
  22. bobcccc

    bobcccc

    Joined:
    Mar 12, 2014
    Posts:
    122
    Ok no idea what I did but now it seems to work everytime...Installed fiddler and went to test and after 150 tries it never returns null now.....
     
  23. bobcccc

    bobcccc

    Joined:
    Mar 12, 2014
    Posts:
    122
    Ok after some further testing when fiddler is active it works everytime, when fiddler is not active it sometimes fails and returns null......
     
  24. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,722
    Check the responseCode on UWR after empty response.
    Also, could you show the raw request and response from Fiddler?
     
  25. bobcccc

    bobcccc

    Joined:
    Mar 12, 2014
    Posts:
    122
    I just checked in unity and when the data returns null it returns a response code of 0.
     
  26. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,722
    Could you show how raw request and response looks like in Fiddler?
     
  27. bobcccc

    bobcccc

    Joined:
    Mar 12, 2014
    Posts:
    122
    In fiddler the request always works, I wonder if it is becuase here in thailand the internet is shared ips and not dedicated.

    In fiddler the raw request is

    POST http://server.com/dostuff.php HTTP/1.1
    Host: server.com
    User-Agent: UnityPlayer/2019.3.7f1 (UnityWebRequest/1.0, libcurl/7.52.0-DEV)
    Accept: */*
    Accept-Encoding: identity
    Connection: Keep-Alive
    Cookie: PHPSESSID=blahblahblahblah
    Content-Type: application/x-www-form-urlencoded
    X-Unity-Version: 2019.3.7f1
    Content-Length: 5

    Job=1

    And the raw response is

    HTTP/1.1 200 OK
    Date: Sun, 26 Jul 2020 14:06:18 GMT
    Server: Apache
    Expires: Thu, 19 Nov 1981 08:52:00 GMT
    Cache-Control: no-store, no-cache, must-revalidate
    Pragma: no-cache
    Set-Cookie: HttpOnly;Secure;SameSite=None
    Keep-Alive: timeout=2, max=498
    Connection: Keep-Alive
    Transfer-Encoding: chunked
    Content-Type: text/html; charset=UTF-8

    1e
    1|419|171648|497|0|0|0|3|3|100
    0
     
  28. bobcccc

    bobcccc

    Joined:
    Mar 12, 2014
    Posts:
    122
    After some more testing, I tried using a vpn with a dedicated Ip and it still returns null sometimes. When fiddler is running and capturing it NEVER returns null...soon as i turn live capturing off it will return null if i wait approximately 2 seconds to click the button and send another webrequest.....
     
  29. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,722
    Do you have a proxy configured on your machine?
    Fiddler works as a proxy itself, which would explain, why it works with it, but not without it if there are issues with the proxy.
    The only other thing I'd try is to set useHttpContinue property on UWR to false.
     
  30. bobcccc

    bobcccc

    Joined:
    Mar 12, 2014
    Posts:
    122
    I do not have a proxy configured on my machine. I have tried httpcontinue to false and true but neither fixed the problem. I am guessing it is something to do with proxy, I am wondering if this is just a problem for me or all of asia(I am guessing it is a problem with many places in asia seeing same problem happens on my phone and my friends phones)... But not sure why UWR is not returning an error and returning as if the request was completed when it was not.
     
    LuckyMoney likes this.
  31. LuckyMoney

    LuckyMoney

    Joined:
    Apr 29, 2020
    Posts:
    1
    we have same isuue too
     
  32. maratzahidulin

    maratzahidulin

    Joined:
    Jan 5, 2021
    Posts:
    3
    The same.
    Also it happens only when I use WWWForm.
    If without WWWForm - it works fine (returns result string constantly).
     
  33. unity_jSN6Q_iSu5g-0w

    unity_jSN6Q_iSu5g-0w

    Joined:
    Mar 30, 2020
    Posts:
    2
    Same thing happening to me. It returns nothing 1 in 20 times if I request in 2 sec intervals. My php 100% returns something no matter what data I send to it from unity, so it seems it's not even reaching my server on those failed webrequests
     
  34. russallen5

    russallen5

    Joined:
    Jun 7, 2018
    Posts:
    3
    Similar issue in a new project- I have a series of UWRs at start that run sequentially. The first 2 run and return from the server properly, but the second always returns with downloadHandler set to Null.
    My calls check for a connection to the server, get some basic data from the server, then try to log the current user in.
    It doesn't seem to matter which order the calls are in, it's always the 3rd one that fails.

    I use nearly identical code in a handful of other projects. I checked 4 of them and they are all working fine.

    Per the suggestions above, I installed Fiddler and tried while capturing, but I'm still getting a null downloadHandler.

    Raw Request from a working UWR:
    GET https://base.potentialgamestudio.com/ortho/alive.php? HTTP/1.1
    Host: base.potentialgamestudio.com
    User-Agent: UnityPlayer/2020.3.0f1 (UnityWebRequest/1.0, libcurl/7.52.0-DEV)
    Accept: */*
    Accept-Encoding: identity
    X-Unity-Version: 2020.3.0f1



    Raw Response from a working UWR:
    HTTP/1.1 200 OK
    Date: Sat, 05 Jun 2021 05:23:33 GMT
    Server: Apache/2.4.29 (Ubuntu)
    Content-Length: 8
    Content-Type: text/html; charset=UTF-8

    0,2,0.99


    Raw Request from the broken UWR:
    GET https://base.potentialgamestudio.co...ba6395a5864da618d99ad3db60dd&platform=Android HTTP/1.1
    Host: base.potentialgamestudio.com
    User-Agent: UnityPlayer/2020.3.0f1 (UnityWebRequest/1.0, libcurl/7.52.0-DEV)
    Accept: */*
    Accept-Encoding: identity
    X-Unity-Version: 2020.3.0f1


    Raw Response from the broken UWR:
    HTTP/1.1 200 OK
    Date: Sat, 05 Jun 2021 05:23:33 GMT
    Server: Apache/2.4.29 (Ubuntu)
    Content-Length: 21
    Content-Type: text/html; charset=UTF-8

    Player0637,40390637,0
     
    Last edited: Jun 5, 2021
  35. incadawr

    incadawr

    Joined:
    Sep 5, 2017
    Posts:
    1
    I had the same issues a few years ago.
    After some investigations I've decided to add a retry policy in my queries, retry http-queries 3-5 times (if I received null response) and add cache on backend side for identical requests.
     
  36. Jerkins

    Jerkins

    Joined:
    Jul 15, 2016
    Posts:
    7
    fixed it?
     
  37. unity_jSN6Q_iSu5g-0w

    unity_jSN6Q_iSu5g-0w

    Joined:
    Mar 30, 2020
    Posts:
    2
    I have exactly the same issue. UWR returns null as if the php response was null, but the file always returns a value. UWR.isDone is always true, even if it returns null. No UWR error handling alarms as if Unity thinks things went great, when it didn't.

    The problem occurs when running the app from Unity and the same thing happens when using apk on android device. Also on my friends device and he's on the other side of the town, using different ISP.

    Same case for Fiddler - when it's running there is no error, when it's not - one in a few attempts comes back with nothing.
    I think I used all the tricks from the internet and I'll have to retry the UWR if it returns null, but I'm not happy with this workaround.