多部分请求获取错误代码400 java

时间:2017-02-16 18:13:38

标签: java android jax-rs multipartform-data

我正在使用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安卓代码建立连接时,我得到了相同的错误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 个答案:

答案 0 :(得分:0)

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

  

multipart Content-Type的唯一必需参数是   边界参数,由一组中的1到70个字符组成   已知通过电子邮件网关非常强大的字符,而不是   以空白结束。 (如果边界看起来以白色结束   空间,必须假定白色空间已被添加   网关,应该被删除。)它由正式指定   跟随BNF:

     

边界:= 0 * 69 bcharsnospace

     

bchars:= bcharsnospace /“”

     

bcharsnospace:= DIGIT / ALPHA /“'”/“(”/“)”/“+”/“_”                  /“,”/“ - ”/“。” /“/”/“:”/“=”/“?”   Reference

尽管如此,由于我删除了“=”字符,一切都开始正常工作。

OLD BUNDARY: - === 1487292689114 ===

NEW BOUNDARY: - X1487292689114x