在Nginx上使用HttpClient发布数据。错误411 C#

时间:2012-09-01 15:24:38

标签: c# post nginx httpclient

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

使用WampServer可以很好地工作,但是当我尝试使用Nginx时,我遇到了411错误,需要内容长度。

这是我的代码:

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,但我不想修改服务器..

感谢您的帮助..

1 个答案:

答案 0 :(得分:1)

我遇到了类似的问题,但在我的情况下,我将Nginx升级到1.4.x。

nginx(fix-nginx-411-length-required-error

的错误411有多种原因
  • 客户端发送了无效的“Content-Length”标题
  • 客户端已发送...方法没有“Content-Length”标题
  • 客户端发送了“Transfer-Encoding:chunked”标题

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