Since Modified Header in c#

主宰稳场 提交于 2020-01-06 19:48:51

问题


Am saving Webpages from webbrowser control to the directory like below code. Now i want to check all the webpages daily that is modified or not. if it is modified have to update or else leave it. here i tried something in console application.

static void Main(string[] args)
        {

            Uri myUri = new Uri("http://www.google.com");
            // Create a new 'HttpWebRequest' object with the above 'Uri' object.
            HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(myUri);

            // Create a new 'DateTime' object.
            DateTime targetDate = DateTime.Now;
            // Set a target date of a week ago
            targetDate.AddDays(-7.0);
            myHttpWebRequest.IfModifiedSince = targetDate;

            try
            {
                // Assign the response object of 'HttpWebRequest' to a 'HttpWebResponse' variable.
                HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
                Console.WriteLine("Response headers for recently modified page\n{0}\n", myHttpWebResponse.Headers);
                Stream streamResponse = myHttpWebResponse.GetResponseStream();
                StreamReader streamRead = new StreamReader(streamResponse);
                Char[] readBuff = new Char[256];
                int count = streamRead.Read(readBuff, 0, 256);
                Console.WriteLine("\nThe contents of Html Page are :  \n");

                while (count > 0)
                {
                    String outputData = new String(readBuff, 0, count);
                    Console.Write(outputData);
                    count = streamRead.Read(readBuff, 0, 256);
                }
                // Close the Stream object.
                streamResponse.Close();
                streamRead.Close();
                // Release the HttpWebResponse Resource.
                myHttpWebResponse.Close();

                Console.WriteLine("\nPress 'Enter' key to continue.................");
                Console.Read();
            }
            catch (WebException e)
            {
                if (e.Response != null)
                {
                    if (((HttpWebResponse)e.Response).StatusCode == HttpStatusCode.NotModified)
                    {
                        Console.WriteLine("\nThe page has not been modified since " + targetDate);
                        Console.ReadLine();
                    }
                    else
                        Console.WriteLine("\nUnexpected status code = " + ((HttpWebResponse)e.Response).StatusCode);
                      Console.ReadLine();
                }
                else
                    Console.WriteLine("\nUnexpected Web Exception " + e.Message);
                  Console.ReadLine();
            }
        }

I tried this as console application and here i gave www.google.com straightly. but i want to check from my directory which is i saved from web browser control.

 var filename1 = webBrowser1.Document.Title;

        var path1 = (@"D:\Cache\" + filename1 + ".html");
        if (mb != 1)
        {
            if (File.Exists(path1))
            {
                MessageBox.Show("Exist");
            }
            else
            {
                File.WriteAllText(path1, webBrowser1.Document.Body.Parent.OuterHtml, Encoding.GetEncoding(webBrowser1.Document.Encoding));
                MessageBox.Show("Saved");
            }
        }

Anyone help me to finish this application. Thanks in Advance.

来源:https://stackoverflow.com/questions/30525491/since-modified-header-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!