【问题标题】:Multipart request is getting error code 400 java多部分请求得到错误代码 400 java
【发布时间】:2017-02-16 18:13:37
【问题描述】:

我正在使用 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--

这是 Android 的 httpURLConnection:

    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 标头。

【问题讨论】:

  • 它还说了什么?请求错误且没有更多详细信息?
  • 可以在服务器端设置断点吗?
  • @efekctive 我在 JAX-RS 中设置了一个过滤器,以便在处理 servlet 之前打印标题和有效负载,但我无法获得更多信息,因为 servlet 立即拒绝了请求。你想要什么信息?
  • 你能在 servlet 处理之前比较这两个请求吗?
  • 当然,看看版本。我已经尝试添加标题但仍然无法正常工作。只是Referer和Origin没有添加到java android HttpURLConnection请求中。

标签: java android jax-rs multipartform-data


【解决方案1】:

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

multipart Content-Type 的唯一强制参数是 边界参数,由一组 1 到 70 个字符组成 已知通过电子邮件网关非常健壮的字符,而不是 以空白结尾。 (如果边界看起来以白色结尾 空格,必须假定空格是由 网关,并应删除。)它是由正式规定 关注 BNF:

边界 := 0*69 bcharsnospace

bchars := bcharsnospace / " "

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

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

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

新边界:--X1487292689114x

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-08
    • 2017-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多