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

Question Error on http request

Discussion in 'Scripting' started by as_ima, Sep 27, 2022.

  1. as_ima

    as_ima

    Joined:
    Sep 14, 2015
    Posts:
    3
    Please excuse my machine translation, as my English is not very good.

    I have created an application that communicates with a server using http. The application worked as long as there was an Internet connection.

    One day, for a demonstration, I needed to use a LAN that was not connected to the Internet, and when I tried the application, I got a "No Internet Connection" error and the application did not work.

    I can access the server URL in my browser, but when I make a request from the application, I get an error.
    The server address is a local IP address such as "http://192.168.x.x".
    Does this error occur if the LAN is not connected to the Internet? Or is there a workaround?

    Unity version: 2021.3.8f1


    Here is the actual code.

    Code (CSharp):
    1. public IEnumerator OAuth2Co(string hostUrl, string userId, string password, string cKey, string sKey, Action<string> errorCallback = null)
    2. {
    3.     string _requestURL = $"{hostUrl.TrimEnd('/')}/oauth2/token";
    4.  
    5.     WWWForm form = new WWWForm();
    6.     form.AddField("grant_type", "password");
    7.     form.AddField("username", userId);
    8.     form.AddField("password", password);
    9.     form.AddField("client_id", cKey);
    10.     form.AddField("client_secret", sKey);
    11.  
    12.     using (var req = UnityWebRequest.Post(_requestURL, form))
    13.     {
    14.         req.SendWebRequest();
    15.  
    16.         while (!req.isDone)
    17.         {
    18.             yield return null;
    19.         }
    20.  
    21.         string msg = $"result:{req.result}, responseCode :{req.responseCode}, error:{req.error}";
    22.         Debug.Log(msg);
    23.         // output -> result:ConnectionError, responseCode :0, error:No Internet Connection
    24.  
    25.         // Omit the rest of the process
    26.  
    27.  
    28.     }
    29. }
    Any help is most-definitely appreciated.
     
  2. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,912
    Well, it's hard to follow what the actual issue may be. However if you use OAuth to authenticate yourself, don't you use an external service for that? Or does your own server implement OAuth? Because if your local server does perform the authentication over OAuth via an external provider (google, facebook, ... whatever) of course the user can not be authenticated without an internet connection.

    If your server does provide his own OAuth interface, it may be that your server actually needs an internet connection.

    Other common issues which are quite specific for WebGL builds are: When using external libraries and using CDN links to content providers for those libraries, those of course would not be available without internet. That's why I prefer to have all javascript libraries that a webbuild needs inside the build or at least on the own server as well. Any external dependencies of course makes you dependent on that external resource and if not available would break your application.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,514
  4. as_ima

    as_ima

    Joined:
    Sep 14, 2015
    Posts:
    3
    OAuth is implemented on the server side.
    Also, the problem occurs in the same way in UnityEditor and iOS builds, so I do not think it is a build-specific issue.
     
  5. as_ima

    as_ima

    Joined:
    Sep 14, 2015
    Posts:
    3
    I requested this from an environment in which the error occurs using Postman.
    The request was successful and I was able to retrieve the accesstoken.

    I have never done this about comparing traffic, but I will give it a try.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,514
    It's usually some silly HTTP header thingy, like

    Accept-Firstborn-Child: required


    and once you get it added to Unity, it just works.

    Either that or some kind of mime / encoding error.