为什么我得到411长度所需的错误?

时间:2013-08-21 08:15:50

标签: c# .net httprequest http-status-code-411

这就是我用.NET调用服务的方式:

var requestedURL = "https://accounts.google.com/o/oauth2/token?code=" + code + "&client_id=" + client_id + "&client_secret=" + client_secret + "&redirect_uri=" + redirect_uri + "&grant_type=authorization_code";
HttpWebRequest authRequest = (HttpWebRequest)WebRequest.Create(requestedURL);
authRequest.ContentType = "application/x-www-form-urlencoded";
authRequest.Method = "POST";
WebResponse authResponseTwitter = authRequest.GetResponse();

但是当调用此方法时,我得到:

  

异常详细信息:System.Net.WebException:返回远程服务器   错误:(411)所需长度。

我该怎么办?

8 个答案:

答案 0 :(得分:69)

当您使用HttpWebRequest和POST方法时,您必须通过RequestStream设置内容(或者您喜欢的正文)。但是,根据你的代码,使用authRequest.Method =“GET”就足够了。

如果您想知道POST格式,请执行以下操作:

ASCIIEncoding encoder = new ASCIIEncoding();
byte[] data = encoder.GetBytes(serializedObject); // a json object, or xml, whatever...

HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "POST";
request.ContentType = "application/json";
request.ContentLength = data.Length;
request.Expect = "application/json";

request.GetRequestStream().Write(data, 0, data.Length);

HttpWebResponse response = request.GetResponse() as HttpWebResponse;

答案 1 :(得分:24)

您需要在请求标头中添加Content-Length: 0

给出了here

的一个非常具有描述性的测试示例

答案 2 :(得分:12)

当您进行POST HttpWebRequest时,您必须指定要发送的数据的长度,例如:

string data = "something you need to send"
byte[] postBytes = Encoding.ASCII.GetBytes(data);
request.ContentLength = postBytes.Length;

如果您没有发送任何数据,只需将其设置为0,这意味着您只需要在此行中添加代码:

request.ContentLength = 0;

通常情况下,如果您没有发送任何数据,那么选择GET方法会更明智,正如您在HTTP RFC

中所看到的那样

答案 3 :(得分:3)

var requestedURL = "https://accounts.google.com/o/oauth2/token?code=" + code + "&client_id=" + client_id + "&client_secret=" + client_secret + "&redirect_uri=" + redirect_uri + "&grant_type=authorization_code";
HttpWebRequest authRequest = (HttpWebRequest)WebRequest.Create(requestedURL);
authRequest.ContentType = "application/x-www-form-urlencoded";
authRequest.Method = "POST";
//Set content length to 0
authRequest.ContentLength = 0;
WebResponse authResponseTwitter = authRequest.GetResponse();

ContentLength属性包含要作为请求的Content-length HTTP标头发送的值。

ContentLength属性中除 -1 之外的任何值都表示请求上传数据,并且只允许在Method属性中设置上传数据的方法。

ContentLength属性设置为值后,必须将该字节数写入通过调用GetRequestStream方法或BeginGetRequestStreamEndGetRequestStream方法返回的请求流中Error: Can't call non-function: undefined at f.call (94f45a0d5ba7150d1744831d8acd9020a2ddee9c.js:115) at f.mustacheImpl (94f45a0d5ba7150d1744831d8acd9020a2ddee9c.js:115) at Object.f.mustache (94f45a0d5ba7150d1744831d8acd9020a2ddee9c.js:115) at HTML.LI.HTML.DIV.HTML.DIV.HTML.DIV.HTML.IMG.src (94f45a0d5ba7150d1744831d8acd9020a2ddee9c.js:314) at Object.c._withCurrentView (94f45a0d5ba7150d1744831d8acd9020a2ddee9c.js:63) at c._HTMLJSExpander.def.visitAttribute (94f45a0d5ba7150d1744831d8acd9020a2ddee9c.js:63) at i.TransformingVisitor.def.visitAttributes (94f45a0d5ba7150d1744831d8acd9020a2ddee9c.js:57) at c._HTMLJSExpander.def.visitAttributes (94f45a0d5ba7150d1744831d8acd9020a2ddee9c.js:63) at Object.c._expandAttributes (94f45a0d5ba7150d1744831d8acd9020a2ddee9c.js:63) at f (94f45a0d5ba7150d1744831d8acd9020a2ddee9c.js:63) 方法。

有关详细信息,请点击here

答案 4 :(得分:1)

嘿我正在使用Volley并且收到服务器错误411,我在getHeaders方法中添加了以下行:

params.put("Content-Length","0");

它解决了我的问题

答案 5 :(得分:1)

将请求方法的方式从POST更改为GET。

答案 6 :(得分:0)

Google search

2nd result

System.Net.WebException: The remote server returned an error: (411) Length Required.
     

这是尝试拨打电话时出现的一个非常常见的问题   基于REST的API方法通过POST。幸运的是,有一个简单的解决方案   这个。

     

这是我用来调用Windows Azure Management API的代码。   此特定API调用要求将请求方法设置为   POST,但是没有需要发送的信息   服务器

var request = (HttpWebRequest) HttpWebRequest.Create(requestUri);
request.Headers.Add("x-ms-version", "2012-08-01"); request.Method =
"POST"; request.ContentType = "application/xml";
  

要修复此错误,请在请求中添加明确的内容长度   在进行API调用之前。

     

request.ContentLength = 0;

答案 7 :(得分:0)

当我从fiddler捕获的会话中导入Web请求到Visual Studio webtests时,我遇到了同样的错误。某些POST请求没有StringHttpBody标记。我向他们添加了一个空的,错误消失了。在Headers标签后添加:

    <StringHttpBody ContentType="" InsertByteOrderMark="False">
  </StringHttpBody>