【发布时间】:2015-07-15 05:53:22
【问题描述】:
我的应用程序中有许多有效的 https POST 连接,它们都在表单部分发送简单的名称/值对。
我一直在使用 https://httpbin.org/post 测试新的通信,它会退回我的请求,以便我查看它的外观。
返回的请求如下所示:
{
"args": {},
"data": "",
"files": {},
"form": {
"data": "Test file #1\nThis file is for testing the file uploading",
"name": "testFile1.txt"
...
},
"headers": {
"Accept": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2",
"Content-Length": "193",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "Java/1.8.0_45"
},
"json": null,
...
"url": "https://httpbin.org/post"
}
我可以使用httpsConn.getOutputStream().write(data) 将数据放入“表单”部分(如上),其中 httpsConn 是我的 HttpsURLConnection,数据是我的名称/值对的字节[]。
我可以将数据放入“文件”部分使用(在其他位中,我认为这些是重要的代码片段)
request.writeBytes("Content-Disposition: form-data; name=\"data\"; filename=\"testFile1.txt\"\r\n");int data;
while ((data = fileInputStream.read()) != -1) {
request.write(data);
}
请求是我的DataOutputStream
上述选项可用于在“表单”或“文件”部分发送文件,但我不太明白它是如何定义应在哪个部分发送信息的。
如何将信息添加到“数据”或“参数”部分?
【问题讨论】: