I need to create and send an HttpPost containing form data, which should look like this:
I'm using apache's httpmime and httpclient (version 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>
This is my code:
...
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);
...
The code is working (no exception is raised) but the post looks like this in whireshark: How it currently looks like
As you can see, there is just a data-block but no "encapsulated multipart part"-parts. The web-application, which is receiving the post-data, needs the data in the "format" shown in the first screenshot. -> What do I need to do to accomplish this?
I tried as well another approach, but it did not solve my problem (no "encapsulated multipart part"-parts):
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);
Any help is highly appreciated!