Cannot send a content-body with this verb-type

前端 未结 4 1480
醉酒成梦
醉酒成梦 2020-11-29 00:32

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

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-29 01:06

    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);
            ...
        }
    }
    

提交回复
热议问题