0

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.

Pradnya Bolli
  • 1,792
  • 1
  • 15
  • 32
Deena
  • 11
  • 1
  • 3
  • Do I understand correctly that you want to check if the file "new" web page is different than the old? Have a look at this question to compare files: http://stackoverflow.com/questions/7931304/comparing-two-files-in-c-sharp – Niels Filter May 29 '15 at 09:11
  • I need to check the webpages from my directory weather its content is changed or not. for ex.. google will change their interface daily.. like that i need to check. in .net there is one function called IfModifedSince is there.. – Deena May 29 '15 at 09:30

0 Answers0