[英]HTTP POST query how to calculate content length
I am making a HTTP POST query to a server and I am creating the post body manually.我正在对服务器进行 HTTP POST 查询,并且我正在手动创建帖子正文。 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.
我想我在内容长度 header 上犯了一些错误,因为在服务器端,当我一开始收到 http 响应时,我看到标头带有 http 响应 200,然后在我的 php 脚本中打印发布参数和文件名我得到了正确的值,但还有一些垃圾字节。 Here is the body of my http 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--"
);
Here is the output from the server(ignore [0.0] - it comes from the console where I print the result)这是来自服务器的 output(忽略 [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
And the php script on the server which is as simple as you can think of:和服务器上的 php 脚本一样简单,你可以想到:
<?php
print_r($_POST) ;
?>
From the HTTP RFC ( RFC2626, 14.3 ) 来自HTTP RFC( RFC2626,14.3 )
The Content-Length entity-header field indicates the size of the entity-body, in decimal number of OCTETs, sent to the recipient or, in the case of the HEAD method, the size of the entity-body that would have been sent had the request been a GET.
Content-Length实体头字段指示实体主体的大小,以十进制数量的OCTET发送给接收者,或者在HEAD方法的情况下,实体主体的大小已经发送到请求是GET。
In other words you should count the number of bytes ( octets
), therefor \\r\\n
should be considered to be 2 octets/bytes. 换句话说,你应该计算字节数(
octets
),因此\\r\\n
应该被认为是2个八位字节/字节。
String boundary = "AaB03x";
String body = "--" + boundary + "\r\n"
+ "Content-Disposition: form-data; name=\"asd\"\r\n"
+ "\r\n"
+ "123\r\n"
+ "--" + boundary + "\r\n"
+ "Content-Disposition: form-data; name=\"pics\"; filename=\"file1.txt\"\r\n"
+ "Content-Type: text/plain\r\n"
+ "\r\n"
+ "555\r\n"
+ "--" + boundary + "--";
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=\"" + boundary + "\"\r\n"
+ "Content-Length: " + body.length() + "\r\n"
+ "\r\n"
+ body
);
...I would say is the way it should be done ......我想说的是应该这样做的方式
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.
许多现代 HTTP/REST 框架和 API 将自动处理 Content-Length 和其他计算。
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.我的经验是 Content-Length 必须是字节特定的,并且必须在发出请求时计算,因为数据可能是动态的并且在请求之间发生变化。
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.
例如,Content-Type header 将影响有效负载的编码方式。 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 as multipart/form-data
.如果发送正文
application/x-www-form-urlencoded
,则正文的大小将与编码并作为multipart/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.
正如 Filip Roséen 所指出的,任何行/控制字符 (\n \r) 也必须包含在计算中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.