多部分请求收到错误代码 400 java

Multipart request is getting error code 400 java

我正在使用 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 代码建立连接时,我得到了相同的 error 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 header 定义。

问题是边界。我在文档中发现边界可以由这组字符组成:

The only mandatory parameter for the multipart Content-Type is the boundary parameter, which consists of 1 to 70 characters from a set of characters known to be very robust through email gateways, and NOT ending with white space. (If a boundary appears to end with white space, the white space must be presumed to have been added by a gateway, and should be deleted.) It is formally specified by the following BNF:

boundary := 0*69 bcharsnospace

bchars := bcharsnospace / " "

bcharsnospace := DIGIT / ALPHA / "'" / "(" / ")" / "+" / "_" / "," / "-" / "." / "/" / ":" / "=" / "?" Reference

然而,自从我删除了“=”字符后,一切都开始正常工作了。

旧边界:--===1487292689114===

新边界:--X1487292689114x