Exemple #1
0
        private static void StartUp(int count, AppStartRequest asReq)
        {
            const int tryCount = 2;

            if (count > tryCount)
            {
                return;
            }
            bool failed = false;

            var info = GetAppInfo();
            var ini  = new IniFileOperator(IniFileOperator.IniFileName);

            if (info != null && string.IsNullOrWhiteSpace(info.AppId))
            {
                var guid = Guid.NewGuid();
                info.AppId = guid.ToString();
                ini.WriteValue("Application", "AppId", info.AppId);
            }

            var data = new StartUpInfo(info);

            //asReq.StartUpData = data;

            try
            {
#if DEBUG
                string abc = data.ToQueryString();

                //var abc2 = System.Web.HttpUtility.UrlEncode(abc);
                //Debug.WriteLine(abc);
                //Debug.WriteLine(abc2);
#endif
                byte[] bytes = Encoding.UTF8.GetBytes("data=" + System.Web.HttpUtility.UrlEncode(data.ToQueryString()));
                //byte[] bytes = Encoding.UTF8.GetBytes("data=testdata");

                //TODO remove .aspx
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(RegexToolHelper.ULR_APP_STARTUP);
                request.Method    = "POST";
                request.UserAgent = STR_USEA_AGENT_DEFAULT;
                AppendLanguageInfoToHeader(request);
                request.ContentType   = "application/x-www-form-urlencoded";
                request.ContentLength = bytes.Length;
                System.IO.Stream newStream = request.GetRequestStream();
                // Send the data.
                newStream.Write(bytes, 0, bytes.Length);
                newStream.Close();

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                if (response.StatusCode == HttpStatusCode.OK)
                {
                    Stream strmResp = response.GetResponseStream();
                    if (response.Headers["AuthResult"] == "Success")
                    {
                        if (response.ContentLength > 0 && false == data.SNExists())
                        {
                            string sn = response.Headers["SN"];
                            if (false == string.IsNullOrEmpty(sn))
                            {
                                ini.WriteValue("Application", "SN", sn);
                            }
                        }
                    }

                    if (response.ContentLength > 0)
                    {
                        string content = string.Empty;
                        using (StreamReader reader = new StreamReader(strmResp))
                        {
                            content = reader.ReadToEnd();
                        }

                        if (!string.IsNullOrEmpty(content))
                        {
                            var asr = JsonConvert.DeserializeObject <AppStartResponse>(content);
                            if (asr != null)
                            {
                                if (asr.UpdateInfo != null)
                                {
                                    asReq.UpdateInfo = asr.UpdateInfo;
                                    // ProcessUpdateInfo(asr.UpdateInfo);
                                }
                            }
                        }
                    }
                }
            }
            catch (WebException wex)
            {
                failed = true;
                Debug.WriteLine(wex.Message);
            }
            catch (Exception ex)
            {
                //TODO ....Important
                failed = true;
                Debug.WriteLine(ex.Message);
            }
            finally
            {
                if ((!failed) || count == tryCount)
                {
                    //var x = data.SNVerifyLocally();
                }
            }

            if (failed)
            {
                Thread.Sleep(1000 * (count + 1));
                StartUp(count + 1, asReq);
            }
        }
Exemple #2
0
 public static void StartUp(AppStartRequest asr)
 {
     StartUp(0, asr);
 }