Search Unity

Wait until HTML page isn't an error page.

Discussion in 'Scripting' started by LSThimo, Oct 28, 2018.

  1. LSThimo

    LSThimo

    Joined:
    Sep 14, 2017
    Posts:
    5
    Hey guys! I'm writing a simple script that eventually leads you to a site, but sometimes the site goes to an error page if you connect to it. When this happens, I'd like the script to wait to actually open the site when it's not on the error page anymore.
    This is my current code, and right now if it is on the error page it will stay in the while loop until I start the co-routine again.

    Code (CSharp):
    1.     IEnumerator GetSite (string url) {
    2.  
    3.         using (WWW www = new WWW (url)) {
    4.             yield return www;
    5.  
    6.             //Get source code
    7.             string wwwText = www.text;
    8.  
    9.             //Get title
    10.             string firstString = "<title>";
    11.             string secondString = "</title>";
    12.             int pos1 = wwwText.IndexOf (firstString) + firstString.Length;
    13.             int pos2 = wwwText.IndexOf (secondString);
    14.  
    15.             string title = wwwText.Substring (pos1, pos2 - pos1);
    16.             print (title);
    17.  
    18.             //Check if the page is an error page (the error page has 503 in its title)
    19.             while (title.Contains ("503")) {
    20.                 print ("You got a 503 error, waiting until it goes away!");
    21.                 yield return null;
    22.             }
    23.  
    24.             //Open URL
    25.             Application.OpenURL (www.url);
    26.  
    27.         }
    28.  
    29.     }
     
  2. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    If you can - use UnityWebRequest instead. You can retrieve errorCode without parsing the result.

    Anyhow, you'd need to send continious requests to the site to check if the page got an error. Right now you're sending only one request. Something like this:
    Code (CSharp):
    1.     IEnumerator GetSite (string url) {
    2.        string title = string.Empty;
    3.        do {
    4.             using (WWW www = new WWW (url)) {
    5.                    yield return www;
    6.                    //Get source code
    7.                    string wwwText = www.text;
    8.                    //Get title
    9.                    string firstString = "<title>";
    10.                    string secondString = "</title>";
    11.                    int pos1 = wwwText.IndexOf (firstString) + firstString.Length;
    12.                    int pos2 = wwwText.IndexOf (secondString);
    13.                    title = wwwText.Substring (pos1, pos2 - pos1);
    14.            }
    15.      } (while (title.Contains ("503"));
    16.            //Open URL
    17.           Application.OpenURL (www.url);
    18.     }
    19.