gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 02:52:38 24 4
gpt4 key购买 nike

我正在向服务器发出 HTTP POST 查询并手动创建帖子正文。我想我在内容长度 header 上犯了一些错误,因为在服务器端,当我一开始收到 http 响应时,我看到带有 http 响应 200 的 header ,然后在我的 php 脚本中打印发布参数和文件名我得到了正确的值,但还有一些垃圾字节。这是我的 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] - 它来 self 打印结果的控制台)

[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) ;
?>

最佳答案

来自 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.

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

关于java - HTTP POST查询如何计算内容长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8456030/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com