views:

12

answers:

1

Hello,

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?

Thanks, brian

        //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);
+5  A: 

Don't get the request stream, quite simply. GET requests don't have bodies - 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);
        ...
    }
}
Jon Skeet
indeed that is exactly what i wanted to do. thanks.
sweeney