title: C# - Get Response from WebRequest and handle status codes
body: <p>I am writing an updatesystem for .NET-applications and at the moment I am stuck. I try to get a file on a remote server and its content. For that I want to use a HttpWebRequest to get the content and the status code if the operation fails.</p>
  
  <p>I built a function that contains a switch-query and each part asks the status code and does an action then.</p>
  
  <p>This looks like the following:</p>
  
  <pre><code>public void AskStatusCode(int code)
  {
    switch (code)
    {
    case 404:
       // Do an action
       break;
  
    case 405:
       // Do an action
       break;
    }
  }
  </code></pre>
  
  <p>Ok, that is it. Now I created a HttpWebRequest and a HttpWebResponse.</p>
  
  <pre><code>HttpWebRequest requestChangelog = (HttpWebRequest)HttpWebRequest.Create(url);
  requestChangelog.Method = "GET";
  
  HttpWebResponse changelogResponse = (HttpWebResponse)requestChangelog.GetResponse();
  
  // Now call the function and set the status code of the response as parameter.
  AskStatusCode((int)changelogResponse.StatusCode);
  </code></pre>
  
  <p>So, the theory should work, but it does not. It will not do any actions I put in the "case"-block for a special status code.</p>
  
  <p>I removed the file from the remote server to test if it will execute the case-block for code <strong>"404"</strong>, but it always shows me an exception (remote server answered 404), but not that what I wanted this status code to handle with.</p>
  
  <p>So, my question is, why is this not working? The types are integers and I casted the status code to an Int32 as well, as you could see...</p>
  
  <p>To your info: After the status code had been checked and if it is ok, I want to read the content with a stream reader and the ResponseStream.</p>
  
  <p>Help would be appreciated.
  Excuse me, if you did not understand that, I tried to say it as clear as I could.</p>