用于分块编码的HTTP标头POST - 错误411

时间:2013-06-22 21:36:12

标签: http http-headers arduino http-post http-error

我正在向Arduino which requires constructing an HTML POST line-by-line的服务器发送数据。我不一定知道Content-Length a-priori,所以我使用“chunked”编码。

当我使用this example post from Wikipedia

尝试"Transfer-Encoding" option as specified in rfc2616
client.println("POST /myurl HTTP/1.1");
client.println("Host: 12.345.679.999"); // replaced with the test server's IP
client.println("User-Agent: Arduino/1.0");
client.println("Transfer-Encoding: chunked");
client.println();  
client.println("4");
client.println("test");
client.println("0");
client.println();

或者,使用转义字符显式:

client.print("4\r\ntest\r\n0\r\n\r\n");

我从服务器收到错误:

HTTP/1.1 411 Length Required
A request of the requested method POST requires a valid Content-length.
Server: Apache/2.2.22 (Ubuntu)

但是,“chunked”编码不应要求Content-Length标头字段,请参阅4.4 - Message Length in rfc2616

我错过了一个字段吗?为什么这个电话不起作用?

对于记录,非Chunked-Encoding有效:

if(client.connect(server, 80)){
    String PostData = "test";
    Serial.println("POST /myurl HTTP/1.1");
    client.println("Host: 12.345.679.999"); // replaced with the test server's IP
    Serial.println("User-Agent: Arduino/1.0");
    Serial.print("Content-Length: ");
    Serial.println(PostData.length());
    Serial.println();
    Serial.println(PostData);
}

更新

来自apache2 error.log:“chunked Transfer-Encoding forbidden”

1 个答案:

答案 0 :(得分:12)

找到后

chunked Transfer-Encoding forbidden

在我的Apache2日志中,我得出结论,错误不在我正在进行的POST中。

我发现modwsgi(apache和django之间的中间层)默认情况下不启用分块传输编码。 In the past, chunked wasn't supported at all

Refering to the change-log in the new version of modwsgi,我发现写作

WSGIChunkedRequest On

在我的apache httpd.conf文件中允许分块请求(不再有411错误)