CodeImp.Gluon.TreintijdenDisplayPanel.UpdateThread C# (CSharp) Метод

UpdateThread() приватный Метод

private UpdateThread ( ) : void
Результат void
        private void UpdateThread()
        {
            HttpWebResponse webresponse = null;
            bool failed = false;
            string failmsg = "";

            ASCIIEncoding encoding = new ASCIIEncoding();
            byte[] bytedata = encoding.GetBytes(POST_DATA);

            // Make the POST request
            HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(ADDRESS);
            webrequest.Referer = REFERRER;
            webrequest.Method = "POST";
            webrequest.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
            webrequest.ContentLength = POST_DATA.Length;
            webrequest.KeepAlive = true;
            webrequest.Timeout = 4000;
            webrequest.Accept = "text/xml,application/xml";
            webrequest.UserAgent = "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.14) Gecko/2009082707 Firefox/3.0.14 GTB5 (.NET CLR 3.5.30729)";
            webrequest.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);

            try
            {
                // Add the POST data
                Stream webrequeststream = webrequest.GetRequestStream();
                webrequeststream.Write(bytedata, 0, POST_DATA.Length);
                webrequeststream.Close();

                // Download!
                webresponse = (HttpWebResponse)webrequest.GetResponse();
            }
            catch(Exception e)
            {
                failed = true;
                failmsg = "Failed to receive treintijden. Web request " + e.GetType().Name + ": " + e.Message;
                webresponse = null;
            }

            // Check result
            if(!failed)
            {
                if((webresponse != null) && (webresponse.StatusCode == HttpStatusCode.OK))
                {
                    // Read entire response
                    StreamReader reader = new StreamReader(webresponse.GetResponseStream());
                    string xmltext = reader.ReadToEnd();

                    // Done with the link
                    webresponse.Close();

                    XmlDocument newxml = new XmlDocument();
                    XmlDocument newhtml = new XmlDocument();
                    try
                    {
                        newxml.LoadXml(xmltext);
                        string htmltext = "<html>" + newxml.DocumentElement.InnerText + "</html>";

                        // Fix what the parser doesn't like
                        htmltext = htmltext.Replace("&copy;", "");
                        htmltext = htmltext.Replace("ns:sitestat", "title");
                        htmltext = htmltext.Replace("<p>", "");
                        htmltext = htmltext.Replace("</p>", "");

                        newhtml.LoadXml(htmltext);

                        xml = newhtml;

                        // 20 sec delay until next update
                        nextupdatedelay = NORMAL_UPDATE_DELAY;
                    }
                    catch(Exception e)
                    {
                        failed = true;
                        failmsg = "Failed to parse treintijden. Web response parse " + e.GetType().Name + ": " + e.Message;
                    }
                }
                else
                {
                    failed = true;
                    if(webresponse != null)
                        failmsg = "Failed to receive treintijden. Web response code " + webresponse.StatusCode + ": " + webresponse.StatusDescription;
                    else
                        failmsg = "Failed to receive treintijden. Web response is NULL.";
                }
            }

            // Handle failure
            if(failed)
                HandleFail(failmsg);

            // We're done updating (either successful or failed)
            UpdateComplete();
        }