问题描述
我正在使用MultiPartUtility请求Android中的Web服务,并且发送的请求是:
POST / HTTP/1.1
connection: Keep-Alive
enctype: multipart/form-data
content-type: multipart/form-data;charset=UTF-8;boundary=--===1487292689114===
accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
accept-encoding: gzip, deflate, br
accept-language: es-419,es;q=0.8,en-GB;q=0.6,en;q=0.4
cache-control: max-age=0
upgrade-insecure-requests: 1
user-agent: Dalvik/1.6.0 (Linux; U; Android 4.4.4; 5042A Build/KTU84P)
host: 192.168.10.171:8080
content-length: 990
payload: ----===1487292689114===
Content-Disposition: form-data; name="new_id"
171
----===1487292689114===
Content-Disposition: form-data; name="file"; filename="IMG_20170216_195118.jpg"
Content-Type: image/jpeg
Content-Transfer-Encoding: binary
????
(
----===1487292689114===--
这是服务:
@POST
@Path("/reportNewImage")
@Consumes(MediaType.MULTIPART_FORM_DATA+"; charset=UTF-8")
@Produces(MediaType.TEXT_PLAIN)
public Response setImage(
@FormDataParam("new_id") String new_id,
@FormDataParam("file") InputStream uploadedInputStream,
@FormDataParam("file") FormDataContentDisposition fileDetail, @Context HttpHeaders headers) {
...
}
但是当从Java android代码建立连接时,我遇到了相同的错误400 Bad Requesting 。 当我从html表单建立连接时,结果成功。 请帮助我,可能是什么原因?
编辑:
使用html表单发出的请求:
POST / HTTP/1.1
Host: example.com
Connection: keep-alive
Content-Length: 6564
Cache-Control: max-age=0
Origin: http://localhost
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/55.0.2883.87 Chrome/55.0.2883.87 Safari/537.36
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryNt8QJSIDDDgznij8
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Referer: http://localhost/form.html
Accept-Encoding: gzip, deflate, br
Accept-Language: es-419,es;q=0.8,en-GB;q=0.6,en;q=0.4
------WebKitFormBoundaryNt8QJSIDDDgznij8
Content-Disposition: form-data; name="new_id"
109
------WebKitFormBoundaryNt8QJSIDDDgznij8
Content-Disposition: form-data; name="file"; filename="apple.jpg"
Content-Type: image/jpeg
????JFIFHH??C
...
???????1s?????K?"T?I)???ti6>?d
????
------WebKitFormBoundaryNt8QJSIDDDgznij8--
这是httpURLConnection Android:
URL url = new URL(requestURL);
httpConn = (HttpURLConnection) url.openConnection();
httpConn.setRequestMethod("POST");
httpConn.setUseCaches(false);
httpConn.setDoOutput(true); // indicates POST method
httpConn.setDoInput(true);
httpConn.setRequestProperty("Content-Type",
"multipart/form-data; boundary=" + boundary);
outputStream = httpConn.getOutputStream();
writer = new PrintWriter(new OutputStreamWriter(outputStream, charset),
true);
writer.flush();
writer.close();
// checks server's status code first
int status = httpConn.getResponseCode();
if (status == HttpURLConnection.HTTP_OK) {
BufferedReader reader = new BufferedReader(new InputStreamReader(
httpConn.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();
httpConn.disconnect();
} else {
throw new IOException("Server returned non-OK status: " + status);
}
编辑2:
我发现只有在编译以下行时才会出现错误:
httpConn.setRequestProperty("Content-Type",
"multipart/form-data; boundary=" + boundary);
否则错误代码为415,因为未定义Content-Type
标头。
1楼
问题是边界。 我在文档中发现边界可以由这组字符组成:
多部分Content-Type的唯一必需参数是border参数,该参数由1至70个字符组成,这些字符集中已知通过电子邮件网关非常健壮的一组字符,并且不以空格结尾。 (如果边界似乎以空白结尾,则必须假定该空白已由网关添加,并且应删除。)以下BNF正式指定了该空白:
边界:= 0 * 69 bcharsnospace
bchars:= bcharsnospace /“”
bcharsnospace:= DIGIT / ALPHA /“'” /“(” /“)” /“ +” /“ _” /“,” /“-” /“。 /“ /” /“:” /“ =” /“?”
不过,由于我删除了“ =”字符,所以一切都开始正常工作。
旧边界: -=== 1487292689114 ===
新边界:-- X1487292689114x