[英]How do I use HttpWebRequest with GET method
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. 我用“GET”方法得到一个ProtocolViolationException
异常。
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. 上面的代码现在可以工作了,我忘了取出导致异常被抛出的Content-Type行。 +1 to tweakt & answer to Jon. +1来调整和回答Jon。
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);
}
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
。
Does it make sense for a GET request to send a Content-Type? 发送内容类型的GET请求是否有意义? Did you try removing the third line? 你试过删除第三行吗?
BeginGetRequestStream is used to get a stream specifically for writing data to the request. BeginGetRequestStream用于获取专门用于将数据写入请求的流。 This is not applicable to GET requests. 这不适用于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 ;-) 士气:阅读文档;-)
It is specified in the documentation for the GetRequestStream that it will throw a ProtocolViolationException if the method is GET. 如果方法是GET,它在GetRequestStream的文档中指定它将抛出ProtocolViolationException。 However, I cannot find anything in the HTTP spec to suggest that this is actually an HTTP protocol violation. 但是,我在HTTP规范中找不到任何建议,这实际上是一个HTTP协议违规。 Consider this a challenge. 认为这是一个挑战。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.