title: How do I use HttpWebRequest with GET method
body: <p>I have the following code which works just fine when the method is "POST", but changing to "GET" doesn't work:</p>
  
  <pre><code>HttpWebRequest request = null;
  request = HttpWebRequest.Create(uri) as HttpWebRequest;
  request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
  request.Method = "POST"; // Doesn't work with "GET"
  
  request.BeginGetRequestStream(this.RequestCallback, null);
  </code></pre>
  
  <p>I get a <code>ProtocolViolationException</code> exception with the "GET" method.</p>
  
  <p><strong>Edit:</strong> After having a look using Reflector, it seems there is an explicit check for the "GET" method, if it's set to that it throws the exception.</p>
  
  <p><strong>Edit2:</strong> I've updated my code to the following, but it still throws an exception when I call EndGetResponse()</p>
  
  <pre><code>if (request.Method == "GET")
  {
      request.BeginGetResponse(this.ResponseCallback, state);
  }
  else
  {
      request.BeginGetRequestStream(this.RequestCallback, state);
  }
  </code></pre>
  
  <p>In my function, ResponseCallback, I have this:</p>
  
  <pre><code>HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult);
  </code></pre>
  
  <p>Which throws the exception as well.</p>
  
  <p><strong>Answer</strong>  </p>
  
  <p>The above code now works, I had forgotten to take out the Content-Type line which was causing the exception to be thrown at the end. +1 to tweakt &amp; answer to Jon.</p>
  
  <p>The working code is now below:</p>
  
  <pre><code>HttpWebRequest request = null;
  request = HttpWebRequest.Create(uri) as HttpWebRequest;
  request.Method = "GET";// Supports POST too
  
  if (request.Method == "GET")
  {
      request.BeginGetResponse(this.ResponseCallback, state);
  }
  else
  {
      request.BeginGetRequestStream(this.RequestCallback, state);
  }
  </code></pre>