예제 #1
0
        public static IEnumerator DoLogin(string username, string password, Action succeed)
        {
            var loginPage = UnityWebRequest.Get(LOGIN_URL);

            Debug.Log(username + ":" + password);

            yield return(loginPage.Send());

            #if UNITY_2017_1_OR_NEWER
            if (loginPage.isNetworkError)
            #else
            if (loginPage.isError)
            #endif
            {
                Debug.Log(loginPage.error);
                yield break;
            }


            var SetCookie     = loginPage.GetResponseHeader("set-cookie");
            var rxCookie      = new Regex("csrftoken=(?<csrf_token>.{64});");
            var cookieMatches = rxCookie.Matches(SetCookie);
            var csrfCookie    = cookieMatches[0].Groups["csrf_token"].Value;

            // get the middleware value
            string          loginPageHtml       = loginPage.downloadHandler.text;
            Regex           rxMiddleware        = new Regex("name='csrfmiddlewaretoken' value='(?<csrf_token>.{64})'");
            MatchCollection middlewareMatches   = rxMiddleware.Matches(loginPageHtml);
            string          csrfMiddlewareToken = middlewareMatches[0].Groups ["csrf_token"].Value;

            /*
             * Make a login request.
             */
            if (mIsTest)
            {
                yield return(new WaitForSeconds(0.3f));
            }

            var form = new WWWForm();

            form.AddField("username", username);
            form.AddField("password", password);
            form.AddField("csrfmiddlewaretoken", csrfMiddlewareToken);

            ObservableWWW.Post(REGISTER_URL, form, new Dictionary <string, string>()
            {
                { "cookie", "csrftoken=" + csrfCookie },
                { "X-CSRFToken", csrfCookie }
            }).Subscribe(result =>
            {
                Log.I(result);
                succeed.InvokeGracefully();
            }, Log.E);
        }
예제 #2
0
        public static void DoGetToken(string username, string password, Action <string> onTokenGetted)
        {
            var form = new WWWForm();

            form.AddField("username", username);
            form.AddField("password", password);

            ObservableWWW.Post("http://liangxiegame.com/api-token-auth/", form)
            .Subscribe(response =>
            {
                Debug.Log(response);

                var token = JObject.Parse(response)["token"].Value <string>();

                onTokenGetted(token);
            });
        }