无法发送内容体与这个动词型动词、内容

2023-09-02 20:45:18 作者:无畏向前

我刚刚得到这个异​​常(ProtocolViolationException)在我的.NET 2.0的应用程序(在Windows Mobile 6标准模拟器上运行)。什么困惑我的是,据我所知,我没有添加任何内容主体,除非我已经在不经意间做了不知。我的code是低于(很简单)。还有什么我需要做说服.NET,这仅仅是一个HTTP GET?

谢谢, 布莱恩

  //运行得抢响应
WebRequest的请求= WebRequest.Create(get.AbsoluteUri +参数);
request.Method =GET;
流流= request.GetRequestStream(); //< =这里爆炸
XmlTextReader的读者= XmlTextReader的新(流);
 

解决方案

不要请求流,很简单。 GET请求不要的一般的有机构(即使它的技术上并不禁止HTTP )和的WebRequest 不支持它 - 但是这就是叫 GetRequestStream 为,提供体数据的请求。

既然你想的读的从流,它看起来像你对我真的想获得的响应的并读取该响应流:

 的WebRequest请求= WebRequest.Create(get.AbsoluteUri +参数);
request.Method =GET;
使用(WebResponse类响应= request.GetResponse())
{
    使用(流流= response.GetResponseStream())
    {
        XmlTextReader的读者= XmlTextReader的新(流);
        ...
    }
}
 

首例全平台远程访问木马问世 售价25至50美元

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

解决方案

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