C# 如何将 HttpWebRequest 与 GET 方法一起使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/253549/
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
How do I use HttpWebRequest with GET method
提问by Mark Ingram
I have the following code which works just fine when the method is "POST", but changing to "GET" doesn't work:
我有以下代码在方法为“POST”时工作正常,但更改为“GET”不起作用:
HttpWebRequest request = null;
request = HttpWebRequest.Create(uri) as HttpWebRequest;
request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
request.Method = "POST"; // Doesn't work with "GET"
request.BeginGetRequestStream(this.RequestCallback, null);
I get a ProtocolViolationException
exception with the "GET" method.
ProtocolViolationException
“GET”方法出现异常。
Edit:After having a look using Reflector, it seems there is an explicit check for the "GET" method, if it's set to that it throws the exception.
编辑:使用 Reflector 查看后,似乎对“GET”方法进行了显式检查,如果它设置为抛出异常。
Edit2:I've updated my code to the following, but it still throws an exception when I call EndGetResponse()
Edit2:我已将代码更新为以下内容,但在调用 EndGetResponse() 时仍会引发异常
if (request.Method == "GET")
{
request.BeginGetResponse(this.ResponseCallback, state);
}
else
{
request.BeginGetRequestStream(this.RequestCallback, state);
}
In my function, ResponseCallback, I have this:
在我的函数 ResponseCallback 中,我有这个:
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult);
Which throws the exception as well.
这也会引发异常。
Answer
回答
The above code now works, I had forgotten to take out the Content-Type line which was causing the exception to be thrown at the end. +1 to tweakt & answer to Jon.
上面的代码现在可以工作了,我忘记取出导致最后抛出异常的 Content-Type 行。+1 调整并回答乔恩。
The working code is now below:
工作代码现在如下:
HttpWebRequest request = null;
request = HttpWebRequest.Create(uri) as HttpWebRequest;
request.Method = "GET";// Supports POST too
if (request.Method == "GET")
{
request.BeginGetResponse(this.ResponseCallback, state);
}
else
{
request.BeginGetRequestStream(this.RequestCallback, state);
}
采纳答案by Jon Skeet
This is specified in the documentation. Basically GET requests aren't meant to contain bodies, so there's no sensible reason to call BeginGetRequestStream
.
这是在文档中指定的。基本上 GET 请求并不意味着包含正文,因此没有合理的理由调用BeginGetRequestStream
.
回答by Mark Renouf
Does it make sense for a GET request to send a Content-Type? Did you try removing the third line?
GET 请求发送 Content-Type 是否有意义?您是否尝试删除第三行?
回答by Tor Haugen
BeginGetRequestStream is used to get a stream specifically for writing data to the request. This is not applicable to GET requests.
BeginGetRequestStream 用于获取专门用于向请求写入数据的流。这不适用于 GET 请求。
The documentation for the BeginGetRequestStream method states explicitly that the method will throw a ProtocolViolationException if the Method is GET or HEAD.
BeginGetRequestStream 方法的文档明确指出,如果 Method 是 GET 或 HEAD,则该方法将抛出 ProtocolViolationException。
Morale: read the docs ;-)
士气:阅读文档 ;-)
回答by MarkPflug
It is specified in the documentation for the GetRequestStream that it will throw a ProtocolViolationException if the method is GET. However, I cannot find anything in the HTTP specto suggest that this is actually an HTTP protocol violation. Consider this a challenge.
在 GetRequestStream 的文档中指定,如果方法是 GET,它将抛出 ProtocolViolationException。但是,我在HTTP 规范中找不到任何内容表明这实际上是违反 HTTP 协议的。认为这是一个挑战。