Esempio n. 1
0
        private string Login(string loginUrl, string userName, string password = "", string userToken = "")
        {
            string errMsg = "Invalid input";

            try
            {
                HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(loginUrl);
                httpWebRequest.ContentType = "text/plain";
                httpWebRequest.Method      = "POST";
                httpWebRequest.KeepAlive   = false;

                using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                {
                    LoginRequest request = new LoginRequest();
                    request.UserName  = userName;
                    request.Password  = password;
                    request.UserToken = userToken;

                    string json = WebMessage.ToJsonString <LoginRequest>(request);

                    streamWriter.Write("login/login/" + json);
                    streamWriter.Flush();

                    //streamWriter.Close();
                }

                string          responseJson = "";
                HttpWebResponse response     = (HttpWebResponse)httpWebRequest.GetResponse();
                using (StreamReader sr = new StreamReader(response.GetResponseStream()))
                {
                    responseJson = sr.ReadToEnd();

                    LogMsg(responseJson);
                }

                errMsg = "Abnormal reply";
                if (responseJson != null && responseJson.Length > 0)
                {
                    LoginReply reply = WebMessage.ToJsonObject <LoginReply>(responseJson);
                    if (reply != null && reply.Result != null)
                    {
                        if (reply.Result.ToLower() == "ok")
                        {
                            m_UserName  = userName;
                            m_UserToken = reply.UserToken;

                            m_Client.Open((m_Client.HasValidationCallback() ? "wss://" : "ws://") + reply.ServerUri);

                            return("");
                        }

                        errMsg = reply.Result;
                    }
                }
            }
            catch (Exception ex)
            {
                errMsg = ex.Message;
            }

            return(errMsg);
        }