109

I just got this exception (ProtocolViolationException) in my .NET 2.0 app (running on windows mobile 6 standard emulator). What confuses me is that as far as i know, I have not added any content body, unless I've inadvertently done it somehow. My code is below (very simple). Is there anything else i need to do to convince .NET that this is just a http GET?

//run get and grab response
WebRequest request = WebRequest.Create(get.AbsoluteUri + args);
request.Method = "GET";
Stream stream = request.GetRequestStream();           // <= explodes here
XmlTextReader reader = new XmlTextReader(stream);
CharithJ
  • 46,289
  • 20
  • 116
  • 131
Brian Sweeney
  • 6,693
  • 14
  • 54
  • 69

4 Answers4

177

Don't get the request stream, quite simply. GET requests don't usually have bodies (even though it's not technically prohibited by HTTP) and WebRequest doesn't support it - but that's what calling GetRequestStream is for, providing body data for the request.

Given that you're trying to read from the stream, it looks to me like you actually want to get the response and read the response stream from that:

WebRequest request = WebRequest.Create(get.AbsoluteUri + args);
request.Method = "GET";
using (WebResponse response = request.GetResponse())
{
    using (Stream stream = response.GetResponseStream())
    {
        XmlTextReader reader = new XmlTextReader(stream);
        ...
    }
}
Community
  • 1
  • 1
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 19
    Technically, GET requests can have bodies, but .NET does not support generating GET reqeusts with bodies. http://stackoverflow.com/questions/2064281/sending-post-data-with-get-request-valid – mhud Jul 10 '12 at 22:23
  • 1
    @JonSkeet You must be a genius or something. That's exactly what solved my problem. +1. – Eternal Learner Oct 25 '12 at 14:38
  • 4
    @EternalLearner You have that backwards. The proper expression is, "Genius, you must be a Skeet or something." :) This sorted me out too, and I gave the responder a +1 before I realized I was helping Mr. Skeet to 1M. – Dan Solovay May 02 '14 at 01:10
  • 2
    @JonSkeet, is there a way to send a GET request with a content body using `HttpClient` and `HttpRequestMessage`? – Shimmy Weitzhandler Feb 23 '18 at 00:51
  • 2
    @JonSkeet NM I got my answer [here](https://stackoverflow.com/questions/11091160/rest-api-get-request-with-body). – Shimmy Weitzhandler Feb 23 '18 at 01:18
5

I had the similar issue using Flurl.Http:

Flurl.Http.FlurlHttpException: Call failed. Cannot send a content-body with this verb-type. GET http://******:8301/api/v1/agents/**** ---> System.Net.ProtocolViolationException: Cannot send a content-body with this verb-type.

The problem was I used .WithHeader("Content-Type", "application/json") when creating IFlurlRequest.

flam3
  • 1,897
  • 2
  • 19
  • 26
2

Because you didn't specify the Header.

I've added an extended example:

var request = (HttpWebRequest)WebRequest.Create(strServer + strURL.Split('&')[1].ToString());

Header(ref request, p_Method);

And the method Header:

private void Header(ref HttpWebRequest p_request, string p_Method)
{
    p_request.ContentType = "application/x-www-form-urlencoded";
    p_request.Method = p_Method;
    p_request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows CE)";
    p_request.Host = strServer.Split('/')[2].ToString();
    p_request.Accept = "*/*";
    if (String.IsNullOrEmpty(strURLReferer))
    {
        p_request.Referer = strServer;
    }
    else
    {
        p_request.Referer = strURLReferer;
    }
    p_request.Headers.Add("Accept-Language", "en-us\r\n");
    p_request.Headers.Add("UA-CPU", "x86 \r\n");
    p_request.Headers.Add("Cache-Control", "no-cache\r\n");
    p_request.KeepAlive = true;
}
Reactgular
  • 52,335
  • 19
  • 158
  • 208
equiman
  • 7,810
  • 2
  • 45
  • 47
-8

Please set the request Content Type before you read the response stream;

 request.ContentType = "text/xml";
Ankur
  • 5,086
  • 19
  • 37
  • 62