views:

233

answers:

2

I am getting a "System.Net.ProtocolViolationException: Operation is not valid due to the current state of the object." error when trying to call

 var request = (HttpWebRequest)WebRequest.Create(uri);

 request.Method = "GET";

 request.ContentType = "text/xml";

 request.BeginGetRequestStream(RequestCompleted, request);
+2  A: 

I suspect this may be because you are performing a BeginGetRequestStream on a request object for which you have specified the "GET" method.

When performing a "GET" the server will not be expecting an entity body in the request hence you should proceed straight to BeginGetResponse. Also specifying a ContentType on the request is not necessary, it specifies the type of content being sent in the entity body of the request but as stated a "GET" doesn't send any content it only gets content.

AnthonyWJones
so what do I do with a PUT or DELETE then?
cmaduro
@cmaduro: Well PUT (DELETE also doesn't have an entity body) is where you would use `BeginGetRequestStream` however you would nee to be using the ClientHTTP stack since the BrowserHTTP stack only supports GET and POST
AnthonyWJones
I get a security exception.
cmaduro
A: 

I disagree with AnthonyWJones answer. I find nothing in the HTTP spec that prohibits a "GET" request from containing a message body. I think this has unfortunately become the de facto understanding of how HTTP works since there is generally no need (or way) to include a message body. Having said that, he is correct as to the cause of this specific exception. However, I think the BCL should be changed to allow it.

If anyone can point it out I would be very interested to know where the spec precludes this: HTTP RFC 2616

Mark