HTTP POST查询如何计算内容长度
我正在向服务器发出 HTTP POST 查询,并手动创建帖子正文。 我认为我在内容长度标头方面犯了一些错误,因为在服务器端,当我一开始收到http响应时,我看到带有http响应200的标头,然后在我的php脚本中打印post参数和文件名我得到了正确的值,但也有一些垃圾字节。 这是我的 http 帖子的正文:
StringBuffer str = new StringBuffer();
str.append("POST /tst/query.php HTTP/1.1\r\n"
+ "Host: myhost.com\r\n"
+ "User-Agent: sampleAgent\r\n"
+ "Content-type: multipart/form-data, boundary=AaB03x\r\n"
+ "Content-Length: 172\r\n\r\n"
+ "--AaB03x\r\n"
+ "content-disposition: form-data; name=\"asd\"\r\n\r\n123\r\n--AaB03x\r\n"
+ "content-disposition: form-data; name=\"pics\"; filename=\"file1.txt\"\r\n"
+ "Content-Type: text/plain\r\n\r\n555\r\n"
+ "--AaB03x--"
);
这是服务器的输出(忽略 [0.0] - 它来自我打印结果的控制台)
[0.0] HTTP/1.1 200 OK
[0.0] Date: Sat, 10 Dec 2011 11:53:11 GMT
[0.0] Server: Apache
[0.0] Transfer-Encoding: chunked
[0.0] Content-Type: text/html
[0.0]
[0.0] 6
[0.0] Array
[0.0]
[0.0] 2
[0.0] (
[0.0]
[0.0] 1
[0.0]
[0.0] 1
[0.0]
[0.0] 1
[0.0]
[0.0] 1
[0.0]
[0.0] 1
[0.0] [
[0.0] 3
[0.0] asd
[0.0] 5
[0.0] ] =>
3
123
1
2
)
0
以及服务器上的 php 脚本,就像您能想到的一样简单:
<?php
print_r($_POST) ;
?>
I am making a HTTP POST query to a server and I am creating the post body manually.
I think I am making some mistake with the content-length header because on the server side when I get the http response at the beginning I see the headers with http response 200 and then when in my php script I print the post parameters and file names I get the correct values but together with some junk bytes.
Here is the body of my http post:
StringBuffer str = new StringBuffer();
str.append("POST /tst/query.php HTTP/1.1\r\n"
+ "Host: myhost.com\r\n"
+ "User-Agent: sampleAgent\r\n"
+ "Content-type: multipart/form-data, boundary=AaB03x\r\n"
+ "Content-Length: 172\r\n\r\n"
+ "--AaB03x\r\n"
+ "content-disposition: form-data; name=\"asd\"\r\n\r\n123\r\n--AaB03x\r\n"
+ "content-disposition: form-data; name=\"pics\"; filename=\"file1.txt\"\r\n"
+ "Content-Type: text/plain\r\n\r\n555\r\n"
+ "--AaB03x--"
);
Here is the output from the server(ignore [0.0] - it comes from the console where I print the result)
[0.0] HTTP/1.1 200 OK
[0.0] Date: Sat, 10 Dec 2011 11:53:11 GMT
[0.0] Server: Apache
[0.0] Transfer-Encoding: chunked
[0.0] Content-Type: text/html
[0.0]
[0.0] 6
[0.0] Array
[0.0]
[0.0] 2
[0.0] (
[0.0]
[0.0] 1
[0.0]
[0.0] 1
[0.0]
[0.0] 1
[0.0]
[0.0] 1
[0.0]
[0.0] 1
[0.0] [
[0.0] 3
[0.0] asd
[0.0] 5
[0.0] ] =>
3
123
1
2
)
0
And the php script on the server which is as simple as you can think of:
<?php
print_r($_POST) ;
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
自从这个问题发布以来,发生了很多变化。许多现代 HTTP/REST 框架和 API 将自动处理内容长度和其他计算。
我的经验是,内容长度必须是特定于字节的,并且必须在发出请求时计算,因为数据可能是动态的并且在请求之间发生变化。
头球可能会影响身体,所以也要考虑到这一点。例如,Content-Type 标头将影响有效负载的编码方式。如果发送正文
application/x-www-form-urlencoded
,则正文的大小将与编码并作为multipart/form- 发送的请求有很大不同数据
。除此之外,有效负载长度的计算应该相当简单。正如 Filip Roséen 所指出的,任何行/控制字符 (\n \r) 也必须包含在计算中。
A lot has changed since this question was posted. Many of the modern HTTP/REST frameworks and API's will automatically handle Content-Length and other calculations.
My experience is that the Content-Length must be byte-specific, and must be calculated at the time the request is being made, since the data will likely be dynamic and change between requests.
The headers can impact the body, so take that into account as well. For instance, the Content-Type header will affect how the payload is encoded. If the body is being sent
application/x-www-form-urlencoded
, the size of the body will be quite different then a request in which it is encoded and sent asmultipart/form-data
.Aside from that, the calculation of the payload length should be fairly simple. As Filip Roséen indicated, any line/control characters (\n \r) must also be included in the calculation.
...我想说的是应该这样做
...I would say is the way it should be done
来自 HTTP RFC (RFC2626, 14.3)
换句话说,您应该计算字节数 (
八位位组
),因此\r\n
应被视为 2 个八位位组/字节。From the HTTP RFC (RFC2626, 14.3)
In other words you should count the number of bytes (
octets
), therefor\r\n
should be considered to be 2 octets/bytes.