Search Unity

How to create a web session for one-time basic authentication

Discussion in 'Scripting' started by SadevMK, Sep 24, 2021.

  1. SadevMK

    SadevMK

    Joined:
    Nov 1, 2019
    Posts:
    3
    Hi,
    I'm using REST api for getting external data from one of our server. It's use basic authentication. Every time web access, It takes around 2 seconds to load data and within that period, Unity gets stuck. I hope that's happen because of the authentication. Every time we need to authenticate.

    1. Is there anyway to access data by creating a session only after one-time authentication?
    2. Can we use Cookies to do that? If it is possible, please help me by giving sample code.

    Thanks a lot. Any of your suggestions will be a great help.

    Code I used as follows.

    using System;
    using System.Net;
    using System.IO;
    using System.Text;
    public class Program: MonoBehaviour
    {
    void Start()
    {
    StartCoroutine(WebClient());
    }
    IEnumerator WebClient()
    {
    while(true)
    {
    Console.WriteLine("Hello World");
    var request = WebRequest.Create(" https://tunnel-svr-01...........");
    string authInfo = "username" + ":" + "password";
    authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
    //like this:
    request.Headers["Authorization"] = "Basic " + authInfo;
    //var response = request.GetResponse();

    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    Stream resStream = response.GetResponseStream();
    StreamReader reader = new StreamReader(resStream);
    string text = reader.ReadToEnd();
    Console.WriteLine(text);
    Console.ReadLine();
    yield return StartCoroutine("GiveItSomeTime");
    }
    }

    IEnumerator GiveItSomeTime()
    {
    yield return new WaitForSeconds(10);
    }

    }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    Just use UnityWebRequest and yield on the resulting object. See sample code in the docs.

    As always, when debugging anything like this:

    Networking, UnityWebRequest, WWW, Postman, curl, WebAPI, etc:

    https://forum.unity.com/threads/using-unity-for-subscription-lists.1070663/#post-6910289

    https://forum.unity.com/threads/unity-web-request-acting-stupid.1096396/#post-7060150

    And setting up a proxy can be very helpful too, in order to compare traffic:

    https://support.unity.com/hc/en-us/articles/115002917683-Using-Charles-Proxy-with-Unity
     
  3. SadevMK

    SadevMK

    Joined:
    Nov 1, 2019
    Posts:
    3
    Hi Dekker,

    Thank you so much for your prompt replying. Actually I'm new for Unity and Scripting. So that your guidance are new to me and didn't clear much. That's my bad. To get it clear can you please answer bellow points.

    1. The reason of what I'm experience of delay or stuck when accessing web due to usage of wrong web client? If I use UnityWebClient , the stucking experience can be reduce?

    2. Can you please explain a bit more about "how to yield on the resulting object"? Is that mean, wait until the JSON object load ?

    3. Whats the purposes of setting up a proxy? I'm sorry I never work on proxy before. If you can please send me some more references.

    3. Most importantly, are there any way to create a session after one time authentication in Unity? Like we doing in web browsers..( Normally we authenticate one time, after that we can continuously access that web pages without any authentication )
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    1. I suspect your delay is inherent to WebRequest. UnityWebRequest ( UWR) is what is supported officially, you should use it.

    2. You need to look up tutorials on UWR, I'm not retyping them.

    3. To see what is going by.

    4. For auth stuff there is no way I can possibly type out how to do it in a forum. Go work through some basic web auth tutorials, it's a lot to soak in and there's like ten billion different approaches.
     
  5. SadevMK

    SadevMK

    Joined:
    Nov 1, 2019
    Posts:
    3
    Thanks Dekker. As you told, after changing into UnityWebRequest delay was reduced into neglectable level. However I still couldn't to solve auth & web session staff. Please look at the below link. Exactly this is the thing I want to do.

    https://www.veritech.net/how-to-send-requests-unity/

    However It didn't work due to some compeller errors. If anyone can give me a hint that would be really helpful.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    The complete error message contains everything you need to know to fix the error yourself.

    Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors.

    The important parts of the error message are:

    - the description of the error itself (google this; you are NEVER the first one!)
    - the file it occurred in (critical!)
    - the line number and character position (the two numbers in parentheses)
    - also possibly useful is the stack trace (all the lines of text in the lower console window)

    All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don't have to stop your progress and fiddle around with the forum.

    Remember: NOBODY memorizes error codes. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

    See my first post above to debug the actual network transaction for correctness.