Search code examples
c#asp.netwebformshttp-response-codes

HTTP Response Codes C#


Here is the code down below

        List<int> j = new List<int>();
        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(result.SiteURL);
        webRequest.AllowAutoRedirect = false;
        HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
        j.Add((int)response.StatusCode);  

What i want to do is, get all the response codes, seperate them(like 2xx,3xx 4xx-5xx) and put them in different lists. Because i need their numbers like how many 4xx responses are there or how many 200 responses are there. Or is there another way to do it?

result.SiteURL is the URL that for the responses. The problem is the last line of the code doesn't return or get anything. What am i missing here?

edit: The main problem is that whatever i try i only get 1 response code and that is mostly 200:OK. But, for youtube.com(ect) there must be 74 OK(200) responses, 1 No Content(204) response and 2 Moved Permanently(301) responses according to https://tools.pingdom.com/#!/fMjhr/youtube.com. How am i going to get them?


Solution

  • You misunderstand the result shown by pingdom.

    Pingdom requests a web page just like a browser would: It loads the page itself, as well as all resources references by the page: style sheets, scripts, images, etc.

    Your code only loads the main HTML page, which has great availability and always returns 200 OK.

    If you want to reproduce pingdom's results, you'll need to parse the HTML page and load the page's resources as well. Keep in mind that parsing HTML is a non-trivial task (browser vendors put a lot of effort in it), so you might want to reconsider whether this is worth your time.