我正在对服务器进行 HTTP POST 查询,并手动创建帖子正文。我想我在内容长度标头上犯了一些错误,因为在服务器端,当我一开始收到 http 响应时,我看到带有 http 响应 200 的标头,然后在我的 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] - 它来自我打印结果的控制台)
[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) ;
?>
原文由 gop 发布,翻译遵循 CC BY-SA 4.0 许可协议
…我想说的是应该做的方式