Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

Retrieve Date and Time from the Internet

Discussion in 'Connected Games' started by skeptika, Nov 15, 2016.

  1. skeptika

    skeptika

    Joined:
    Dec 31, 2011
    Posts:
    103
    As a way to prevent cheating, I wanted to pull the current Date and Time from the net. So I tried this, and it works just fine in editor. However, when I deploy to WebGL, it gets nothing back in response. The code executes, but returns netTime as a null string. Again, in editor, it works exactly as intended and returns the date/time as a string. WebGL fails. Any ideas why? My understanding is that UnityWebRequest should be working with WebGL per the documentation (I'm using 5.5, it says implemented by 5.3)?

    Code (csharp):
    1.  
    2. public IEnumerator GetInternetTime()
    3. {
    4.     UnityWebRequest myHttpWebRequest = UnityWebRequest.Get("http://www.microsoft.com");
    5.     yield return myHttpWebRequest.Send();
    6.  
    7.     string netTime = myHttpWebRequest.GetResponseHeader("date");
    8.     Debug.Log(netTime + " was response");
    9. }
    netTime returns a nice long string with the date/time in editor, returns nothing though when deployed to WebGL? Am I missing something?
     
    Last edited: Nov 15, 2016
  2. LukeDawn

    LukeDawn

    Joined:
    Nov 10, 2016
    Posts:
    319
    Maybe microsoft's server doesn't like the webplayer's user-agent string? Or are there other restrictions on what a webplayer can do?
     
  3. skeptika

    skeptika

    Joined:
    Dec 31, 2011
    Posts:
    103
    Tried multiple sites (besides MS), they all work locally, none work in WebGL. My guess atm is it works locally b/c there is no cross-domain issue (CORS), but once in browser, it's hitting a CORS issue. I'm trying a CORS proxy to see if that fixes it.
     
  4. skeptika

    skeptika

    Joined:
    Dec 31, 2011
    Posts:
    103
    Nope, even when I use a CORS proxy, it gets rid of the error returned, but I'm still getting absolutely nothing back. Sigh. Any ideas network knowledgeable folks? I'm amenable to alternate solutions if there's a slicker way to get date/time from the net?
     
  5. LukeDawn

    LukeDawn

    Joined:
    Nov 10, 2016
    Posts:
    319
  6. skeptika

    skeptika

    Joined:
    Dec 31, 2011
    Posts:
    103
    Yah it's not CORS, I used https://crossorigin.me to see if that was the problem. It doesn't solve it, I'm getting absolutely nothing back in WebGL, although it seems to work in editor and PC platform...

    I double checked I'm not getting any error with myHttpWebRequest.error is .isError. No error, just null coming back on WebGL. :(

    I even tried using the legacy WWW, and the exact same result. Null.

    Code (csharp):
    1.  
    2. WWW www = new WWW("http://www.whattimeisit.com/");
    3. yield return www;
    4. www.text
    www.text works fine in editor, properly grabs websites text, but in WebGL, nothing but null. True for WWW, true for UnityWebRequest. I'm out of ideas :(
     
    Last edited: Nov 15, 2016
  7. LukeDawn

    LukeDawn

    Joined:
    Nov 10, 2016
    Posts:
    319
  8. skeptika

    skeptika

    Joined:
    Dec 31, 2011
    Posts:
    103
    I actually couldn't get his NTP script to work at all. Even then, the issue isn't that above method doesn't work, it does! It just doesn't work on WebGL!

    That said, I finally got it to return me SOME text using WWW, so I'll just go down that road and see if I can find a website without ridiculous text for me to parse :) Really should work with UnityWebRequest though...

    Oh thank god someone made a reasonable website: http://www.timeapi.org/utc/now

    That's a string I can work with!
     
    Last edited: Nov 15, 2016
  9. Leaton

    Leaton

    Joined:
    Jun 21, 2013
    Posts:
    5
    I had a similar issue and I figured it out then wrote a small guide http://leatonm.net/unity3d-internet-time-and-date-guide-stop-time-cheat/
     
  10. Akhilya

    Akhilya

    Joined:
    Sep 4, 2017
    Posts:
    2
    Code (CSharp):
    1. public static DateTime GetNetTime()
    2.         {
    3.             var myHttpWebRequest = (HttpWebRequest)WebRequest.Create("http://www.microsoft.com");
    4.             var response = myHttpWebRequest.GetResponse();
    5.             string todaysDates = response.Headers["date"];
    6.             return DateTime.ParseExact(todaysDates,
    7.                                        "ddd, dd MMM yyyy HH:mm:ss 'GMT'",
    8.                                        CultureInfo.InvariantCulture.DateTimeFormat,
    9.                                        DateTimeStyles.AssumeUniversal);
    10.         }
     
    PBL1 likes this.
  11. Shafiur_RUL

    Shafiur_RUL

    Joined:
    Jan 23, 2019
    Posts:
    1
    This occur issues on android build. works perfectly on editor. any ideas?