无法发送内容体与这个动词型 [英] Cannot send a content-body with this verb-type

查看:501
本文介绍了无法发送内容体与这个动词型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚得到这个异​​常(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的新(流);
        ...
    }
}
 

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

这篇关于无法发送内容体与这个动词型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆