Reactor httpclient sendForm with Content-Length

问题描述 投票:0回答:0

我正在学习 reactor-netty 作为 http 客户端来发送多表单数据。 我的服务器需要

Content-Length
字段,但似乎 reactor-netty 没有自动设置它。(有趣的是它在每个部分都设置了
Content-Length
,这不满足我的服务器)

HttpClient.create().port(server.port()).post().uri("/postpath")
        .sendForm((req, form) -> form
            .multipart(true)
            .file("a", new ByteArrayInputStream(jsona.getBytes(StandardCharsets.UTF_8)), "application/json")
            .file("b", new ByteArrayInputStream(jsonb.getBytes(StandardCharsets.UTF_8)), "application/json")
        )
        //...omitted, same as the github examples
  • Http流量(错误)
Http traffic is like following:
POST /post HTTP/1.1
user-agent: ReactorNetty/1.1.4
host: myservernmae
accept: */*
content-type: application/json
content-type: multipart/form-data; boundary=40e3ae2412eea9a2
transfer-encoding: chunked

--40e3ae2412eea9a2
content-disposition: form-data; name="a"
content-length: 94
content-type: application/json
content-transfer-encoding: binary

dataa...
--40e3ae2412eea9a2
content-disposition: form-data; name="b"
content-length: 205
content-type: application/json
content-transfer-encoding: binary

datab...
--40e3ae2412eea9a2--

与 http apache 客户端中的

CloseableHttpClient
相比

      final HttpPost httppost = new HttpPost("http://myservernmae/postpath");
      final HttpEntity reqEntity = MultipartEntityBuilder.create()
          .addPart("a", aBody)
          .addPart("b", bBody)
          .build();
      httppost.setEntity(reqEntity);
      final CloseableHttpClient httpclient = HttpClients.createDefault();
      httpclient.execute(httppost, response -> {
      // omit remaining
  • http 流量(正确)
POST /post HTTP/1.1
Accept-Encoding: gzip, x-gzip, deflate
Content-Length: 611
Content-Type: multipart/form-data; charset=ISO-8859-1; boundary=dbiD5AN62qhyG8C8HAbOPu5zlltTOSu2q
Host: myservernmae
Connection: keep-alive
User-Agent: Apache-HttpClient/5.2.1 (Java/11.0.12)

--dbiD5AN62qhyG8C8HAbOPu5zlltTOSu2q
Content-Disposition: form-data; name="a"
Content-Type: application/json; charset=UTF-8

dataa...
--dbiD5AN62qhyG8C8HAbOPu5zlltTOSu2q
Content-Disposition: form-data; name="b"
Content-Type: application/json; charset=UTF-8

datab...
--dbiD5AN62qhyG8C8HAbOPu5zlltTOSu2q--

谁能告诉我怎么设置

Content-Length
?自动更好。我正在使用 jdk11,reactor-netty 1.1.4.

http http-post multipartform-data reactor-netty
© www.soinside.com 2019 - 2024. All rights reserved.