Java 如何使用Apache httpclient创建封装的多部分
我需要创建并发送一个包含表单数据的HttpPost,如下所示: 我正在使用apache的HttpTime和httpclient(版本4.5.10): 代码正在运行(未引发异常),但帖子看起来如下所示,其中共享: 如您所见,只有一个数据块,但没有“封装的多部分”部分。接收post数据的web应用程序需要第一个屏幕截图中显示的“格式”数据。 ->我需要做些什么来实现这一点 我也尝试了另一种方法,但它没有解决我的问题(没有“封装的多部分部件”-部件):Java 如何使用Apache httpclient创建封装的多部分,java,apache,http-post,httpclient,Java,Apache,Http Post,Httpclient,我需要创建并发送一个包含表单数据的HttpPost,如下所示: 我正在使用apache的HttpTime和httpclient(版本4.5.10): 代码正在运行(未引发异常),但帖子看起来如下所示,其中共享: 如您所见,只有一个数据块,但没有“封装的多部分”部分。接收post数据的web应用程序需要第一个屏幕截图中显示的“格式”数据。 ->我需要做些什么来实现这一点 我也尝试了另一种方法,但它没有解决我的问题(没有“封装的多部分部件”-部件): List params=new ArrayL
List params=new ArrayList();
参数添加(新的BasicNameValuePair(“名称123”、“值123”);
setEntity(新的UrlEncodedFormEntity(参数));
HttpClient myClient=HttpClientBuilder.create().disableRedirectHandling().build();
response=myClient.execute(httppost,context);
非常感谢您的帮助
<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);
...
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);