HTTP POST查询如何计算内容长度

发布于 2024-12-20 13:02:16 字数 1258 浏览 2 评论 0原文

我正在向服务器发出 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

原谅过去的我 2024-12-27 13:02:16

自从这个问题发布以来,发生了很多变化。许多现代 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 as 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.

冰之心 2024-12-27 13:02:16
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
);

...我想说的是应该这样做

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

小女人ら 2024-12-27 13:02:16

来自 HTTP RFC (RFC2626, 14.3)

Content-Length 实体标头字段指示发送给接收者的实体主体的大小(以十进制数的八位字节数表示),或者在 HEAD 方法的情况下,指示实体主体的大小如果请求是 GET,则已发送。

换句话说,您应该计算字节数 (八位位组),因此 \r\n 应被视为 2 个八位位组/字节。

From the 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.

In other words you should count the number of bytes (octets), therefor \r\n should be considered to be 2 octets/bytes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文