.net 无法发送具有此动词类型的内容正文
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3981564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot send a content-body with this verb-type
提问by Brian Sweeney
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?
我刚刚在我的 .NET 2.0 应用程序(在 Windows Mobile 6 标准模拟器上运行)中遇到了这个异常 (ProtocolViolationException)。令我困惑的是,据我所知,我没有添加任何内容主体,除非我无意中以某种方式添加了它。我的代码如下(非常简单)。我还需要做些什么来让 .NET 相信这只是一个 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);
回答by Jon Skeet
Don't get the request stream, quite simply. GET requests don't usuallyhave bodies (even though it's not technically prohibited by HTTP) and WebRequestdoesn't support it - but that's what calling GetRequestStreamis for, providing body data for the request.
不要获取请求流,很简单。GET 请求通常没有正文(即使它在技术上没有被 HTTP 禁止)并且WebRequest不支持它 - 但这就是调用的GetRequestStream目的,为请求提供正文数据。
Given that you're trying to readfrom the stream, it looks to me like you actually want to get the responseand 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);
...
}
}
回答by flam3
I had the similar issue using Flurl.Http:
我在使用 Flurl.Http 时遇到了类似的问题:
Flurl.Http.FlurlHttpException: Call failed. Cannot send a content-body with this verb-type. GET http://******:8301/api/v1/agents/**** ---> System.Net.ProtocolViolationException: Cannot send a content-body with this verb-type.
Flurl.Http.FlurlHttpException: Call failed. Cannot send a content-body with this verb-type. GET http://******:8301/api/v1/agents/**** ---> System.Net.ProtocolViolationException: Cannot send a content-body with this verb-type.
The problem was I used .WithHeader("Content-Type", "application/json")when creating IFlurlRequest.
问题是我.WithHeader("Content-Type", "application/json")在创建 IFlurlRequest 时使用的。
回答by equiman
Because you didn't specify the Header.
因为你没有指定标题。
I've added an extended example:
我添加了一个扩展示例:
var request = (HttpWebRequest)WebRequest.Create(strServer + strURL.Split('&')[1].ToString());
Header(ref request, p_Method);
标题(参考请求,p_Method);
And the method Header:
和方法标题:
private void Header(ref HttpWebRequest p_request, string p_Method)
{
p_request.ContentType = "application/x-www-form-urlencoded";
p_request.Method = p_Method;
p_request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows CE)";
p_request.Host = strServer.Split('/')[2].ToString();
p_request.Accept = "*/*";
if (String.IsNullOrEmpty(strURLReferer))
{
p_request.Referer = strServer;
}
else
{
p_request.Referer = strURLReferer;
}
p_request.Headers.Add("Accept-Language", "en-us\r\n");
p_request.Headers.Add("UA-CPU", "x86 \r\n");
p_request.Headers.Add("Cache-Control", "no-cache\r\n");
p_request.KeepAlive = true;
}
回答by Naveen Kumar
Please set the request Content Type before you read the response stream;
请在阅读响应流之前设置请求内容类型;
request.ContentType = "text/xml";

