我刚刚在我的.NET 2.0应用程序中运行此异常(ProtocolViolationException)(在Windows Mobile 6标准仿真器上运行).令我困惑的是,据我所知,我没有添加任何内容正文,除非我无意中以某种方式完成了它.我的代码如下(非常简单).还有什么我需要做的是说服.NET这只是一个http GET吗?
谢谢,布莱恩
//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);
Run Code Online (Sandbox Code Playgroud)
Jon*_*eet 147
不要简单地获取请求流.GET请求通常不具有正文(即使它在技术上不受HTTP限制)并且WebRequest不支持它 - 但这就是调用的GetRequestStream内容,为请求提供正文数据.
鉴于您正在尝试从流中读取,它看起来像您实际上想要获取响应并从中读取响应流:
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);
...
}
}
Run Code Online (Sandbox Code Playgroud)
我在使用 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.
问题是我.WithHeader("Content-Type", "application/json")在创建 IFlurlRequest 时使用的。
| 归档时间: |
|
| 查看次数: |
146063 次 |
| 最近记录: |