如何使用Apache httpclient创建封装的多部分

我需要创建并发送一个包含表单数据的HttpPost,其外观应如下所示:

How it should look like

我正在使用apache的httpmime和httpclient(版本4.5.10):

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.10</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpmime -->
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpmime</artifactId>
        <version>4.5.10</version>
    </dependency>

这是我的代码:

...
ContentType contentType = ContentType.create("multipart/form-data");        

multipartentityBuilder builder = multipartentityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

HttpEntity entity = builder.addTextBody("Name 123","Value 123").setContentType(contentType).build();
httppost.setEntity(entity);
HttpClient myClient = HttpClientBuilder.create().disableRedirectHandling().build();
response = myClient.execute(httppost,context);
...

代码正在运行(不会引发任何异常),但是帖子在whireshark中看起来像这样: How it currently looks like

如您所见,只有一个数据块,但没有“封装的多部分”部分。正在接收后期数据的Web应用程序需要第一个屏幕截图中所示的“格式”数据。 ->我需要怎么做才能做到这一点?

我也尝试了另一种方法,但是它不能解决我的问题(没有“封装的多部分”部分):

List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("Name 123","Value 123"));
httppost.setEntity(new UrlEncodedFormEntity(params));
HttpClient myClient = HttpClientBuilder.create().disableRedirectHandling().build();
response = myClient.execute(httppost,context);

我们非常感谢您的帮助!

lcy0851 回答:如何使用Apache httpclient创建封装的多部分

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3124711.html

大家都在问