Exemplo n.º 1
0
        /// <summary>
        /// Checks for updates online.
        /// </summary>
        private static void checkForUpdates()
        {
            // Version info and salt
            uint            salt     = (uint)Salt.GetSalt();
            Version         ver      = Assembly.GetExecutingAssembly().GetName().Version;
            OperatingSystem osi      = Environment.OSVersion;
            string          osVerStr = osi.Version.Major + "." + osi.Version.Minor + "." + osi.Version.Build + "." + osi.Version.Revision;

            // Create web request; make sure we go through user's configured proxy, if any.
            WebRequest req = HttpWebRequest.Create(Magic.UpdateCheckUrl);

            req.Proxy             = WebRequest.GetSystemWebProxy();
            req.Proxy.Credentials = CredentialCache.DefaultCredentials;
            // POST data
            string pdata = Magic.UpdatePostPattern;

            pdata = string.Format(pdata, Magic.UpdateProduct, salt, ver.Major, ver.Minor, osVerStr);
            byte[] data = Encoding.ASCII.GetBytes(pdata);
            // Send POST data
            req.Method        = "POST";
            req.ContentType   = "application/x-www-form-urlencoded";
            req.ContentLength = data.Length;
            using (var stream = req.GetRequestStream())
            {
                stream.Write(data, 0, data.Length);
            }

            // Read response, which is an XML.
            // Try-finally to make sure response is closed.
            XmlDocument xmldoc = null;
            WebResponse resp   = req.GetResponse();

            try
            {
                Stream respStream = resp.GetResponseStream();
                using (MemoryStream memStream = new MemoryStream())
                {
                    byte[] buffer = new byte[4096];
                    int    count  = 0;
                    int    n      = 0;
                    do
                    {
                        n      = respStream.Read(buffer, 0, buffer.Length);
                        count += n;
                        if (n == 0)
                        {
                            break;
                        }
                        memStream.Write(buffer, 0, n);
                    } while (n > 0);

                    respStream.Close();
                    memStream.Seek(0, SeekOrigin.Begin);
                    using (TextReader tr = new StreamReader(memStream))
                    {
                        xmldoc = new XmlDocument();
                        xmldoc.LoadXml(tr.ReadToEnd());
                    }
                }
            }
            finally
            {
                resp.Close();
            }
            // Try and store update info.
            // If this throws, then we got trash; store that there's no update.
            try
            {
                storeUpdateInfo(xmldoc);
            }
            catch
            {
                UpdateInfo.ClearUpdate();
            }
        }