Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用Apache httpclient创建封装的多部分_Java_Apache_Http Post_Httpclient - Fatal编程技术网

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

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

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

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

代码正在运行(未引发异常),但帖子看起来如下所示,其中共享:

如您所见,只有一个数据块,但没有“封装的多部分”部分。接收post数据的web应用程序需要第一个屏幕截图中显示的“格式”数据。 ->我需要做些什么来实现这一点

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

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);