Question

Well the question pretty much explains it. I'm aware the code is very dirty during String.equals, but I just wanted to see if I understood header responses. Clearly I do not, because I watch in my browser and when a new question is added, my program never outputs "Yep it changed" Why is that?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.IO;
using System.Net;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {

            Uri myUri = new Uri("http://stackoverflow.com/questions?sort=newest");

            HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(myUri);



            HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
            string org = myHttpWebResponse.Headers.GetValues("Date")[0];
            string newone = "";

            while (true) //STRICTING FOR TESTING. THIS WOULD BE A Dos ATTACK AS IT NEVER HAS A DELAY BETWEEN REQUESTS.
            {

                myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
                newone = myHttpWebResponse.Headers.GetValues("Date")[0];

                if (!newone.Equals(org))
                    break;
            }


            Console.WriteLine("Yep it changed");
            Console.ReadLine();
        }
    }
}
Was it helpful?

Solution

Your code is checking the same HttpWebResponse, over and over. It's not making new requests, it's only looking at the response from the first (and only request).

From MSDN:

Multiple calls to GetResponse return the same response object; the request is not reissued.

You'll have to put all of the code that makes a request inside the loop, to start the whole request/response process over again.

Also note, that if you were running this code against my website, I would be pretty upset.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top