1

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?

ovuncdeniz4
  • 29
  • 11
  • Possible duplicate of [Getting Http Status code number (200, 301, 404, etc.) from HttpWebRequest and HttpWebResponse](https://stackoverflow.com/questions/1330856/getting-http-status-code-number-200-301-404-etc-from-httpwebrequest-and-ht) - be sure to read **all** of the responses. – mjwills Dec 28 '17 at 07:25
  • Also see https://stackoverflow.com/a/11618739/34092 for the redirect status codes. – mjwills Dec 28 '17 at 07:25
  • `ConcurrentDictionary resp = new ConcurrentDictionary() ` is that correct? It doesn't see the `response.StatusCode` for this line. It says : **'response' is a variable but is used like a type**@mjwills – ovuncdeniz4 Dec 28 '17 at 07:40
  • 1
    `ConcurrentDictionary resp = new ConcurrentDictionary()` The key is the status code, the value is the count. – mjwills Dec 28 '17 at 07:45
  • Thank you sir so much @mjwills – ovuncdeniz4 Dec 28 '17 at 07:48

1 Answers1

1

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.

Heinzi
  • 167,459
  • 57
  • 363
  • 519
  • "If you want to reproduce pingdom's results, you'll need to parse the HTML page and load the page's resources as well." I'm kind of new to programming so how am i going to do that? Could you help me? – ovuncdeniz4 Dec 28 '17 at 08:21
  • That's not really a suitable task for a beginner. If you still want to do it, start by becoming very proficient with HTML and then learning how to parse HTML in C#. – Heinzi Dec 28 '17 at 09:43