0

我正在测试引入新类的新 Framework .Net 4.5,例如 HttpClient。我正在尝试登录 vbulletin 论坛并发布主题。

它使用 WampServer 可以完美运行,但是当我尝试使用 Nginx 时出现 411 错误,需要 Content-Lenght。

这是我的代码:

HttpClient client = new HttpClient();

/* login part skipped, it works */

postData = new List<KeyValuePair<string, string>>();
postData.Add(new KeyValuePair<string, string>("do", "postthread"));
postData.Add(new KeyValuePair<string, string>("f", "8"));
postData.Add(new KeyValuePair<string, string>("loggedinuser", "1"));
postData.Add(new KeyValuePair<string, string>("message", "myMessage"));
postData.Add(new KeyValuePair<string, string>("subject", "mySubject : " + new Random().Next()));
postData.Add(new KeyValuePair<string, string>("securitytoken", secure_id));
postData.Add(new KeyValuePair<string, string>("vbseo_is_retrtitle", "1"));
postData.Add(new KeyValuePair<string, string>("vbseo_retrtitle", "1"));
postData.Add(new KeyValuePair<string, string>("posthash", posthash));
postData.Add(new KeyValuePair<string, string>("poststarttime", poststarttime));

HttpContent content = new FormUrlEncodedContent(postData);
HttpRequestMessage msg3 = new HttpRequestMessage(HttpMethod.Post, "http://localhost/newthread.php?do=postthread&f=8");

// Adding all headers from the resp2, containing cookie value to stay connected
// I think, here is the problem, when passing the 'Transfer-Encoding' and 'Transfer-EncodingChunked' values
foreach (var header in resp2.Headers)
   msg3.Headers.Add(header.Key, header.Value);

msg3.Content = content;
var resp3 = client.SendAsync(msg3).Result;
resp3.EnsureSuccessStatusCode();
string html = resp3.Content.ReadAsStringAsync().Result;

当我看到 msg3.Content.Headers.ContentLength 时,我有一个值.. 所以我不明白为什么 Nginx 会抛出这个错误:/

谷歌搜索后,我看到了http://wiki.nginx.org/HttpChunkinModule但我不想修改服务器..

谢谢你的帮助..

4

1 回答 1

1

我刚刚遇到了类似的问题,但就我而言,我将 Nginx 升级到了 1.4.x。

nginx 出现错误 411 有多种原因(fix-nginx-411-length-required-error

  • 客户端发送了无效的“Content-Length”标头
  • 客户端发送...没有“Content-Length”标头的方法
  • 客户端发送“Transfer-Encoding:chunked”标头

在 nginx 1.3.9 之前,不支持“Transfer-Encoding: chunked”

于 2013-08-15T16:57:25.347 回答