如何使用Apache HttpComponentst创建和发布多部分/混合http请求?
我正在使用Apache HttpComponents v4.3.3(maven httpclient和httpmime)。 我需要上传一个包含一些元数据的文件。 curl命令可以工作,如下所示。
curl -k -i -H“Content-Type:multipart / mixed”-X POST –form’field1 = val1′-form’field2 = val2′-form’file =@somefile.zip; type = application / zip’https://www.some.domain/
我试过模仿这个curl的post如下。
HttpEntity entity = MultiPartEntityBuilder .create() .addPart("field1",new StringBody("val1",ContentType.TEXT_PLAIN)) .addPart("field2",new StringBody("val2",ContentType.TEXT_PLAIN)) .addPart("file", new FileBody(new File("somefile.zip"), ContentType.create("application/zip")) .build(); HttpPost post = new HttpPost("https://www.some.domain"); post.addHeader("Content-Type", "multipart/mixed");
但是,在我使用HttpClient执行HttpPost之后,我得到以下exception(服务器代码也是在Jetty上运行的Java)。
org.apache.commons.fileupload.FileUploadException:请求被拒绝,因为没有找到多部分边界
当我添加一个曲线curl
curl –trace – -k -i -H“Content-Type:multipart / mixed”-X POST –form’field1 = val1′-form’field2 = val2′-form’file =@somefile.zip; type = application / zip’https://www.some.domain/
我看到表单字段/值对被设置为HTTP标头。
内容处理:表格数据; 名称=字段1 …值1
我在这里做错了什么? 任何帮助表示赞赏。
我做了一些修改,做了两件事让代码工作。
- 不再使用addPart(…)
- 不再设置Content-Type标头
这是经过修改的片段,以防万一有人感兴趣。
HttpEntity entity = MultipartEntityBuilder .create() .addTextBody("field1","val1") .addTextBody("field2","val2") .addBinaryBody("file", new File("somefile.zip"),ContentType.create("application/zip"),"somefile.zip") .build(); HttpPost post = new HttpPost("https://www.some.domain"); post.setEntity(entity);
我还将HttpComponents设置为调试模式。
-Dorg.apache.commons.logging.Log = org.apache.commons.logging.impl.SimpleLog -Dorg.apache.commons.logging.simplelog.showdatetime =真 -Dorg.apache.commons.logging.simplelog.log.org.apache.http = DEBUG
事实certificate,每个部分现在都有一个边界。 更好的是,内容类型和边界是自动生成的。
内容类型:multipart / form-data; 边界= 5ejxpaJqXwk2n_3IVZagQ1U0_J_X9MdGvst9n2Tc
这里我的完整代码基于最后的响应,但略有不同,我有相同的错误,但现在工作(感谢简!):
public String sendMyFile(String p_file, String p_dni, String p_born_date) throws Exception { HttpClient httpclient = HttpClientBuilder.create().build(); HttpPost httppost = new HttpPost("http://localhost:2389/TESTME_WITH_NETCAT"); /* Campos del formulario del POST que queremos hacer */ File fileIN = new File(p_file); /* Construimos la llamada */ MultipartEntityBuilder reqEntity = MultipartEntityBuilder.create(); reqEntity .setMode(HttpMultipartMode.BROWSER_COMPATIBLE) .addBinaryBody ("p_file" , fileIN) .addTextBody ("p_dni" , p_dni) .addTextBody ("p_born_date" , p_born_date); httppost.setEntity(reqEntity.build()); System.out.println("executing request " + httppost.getRequestLine()); HttpResponse response = httpclient.execute(httppost); System.out.println("1 ----------------------------------------"); System.out.println(response.getStatusLine()); System.out.println("2 ----------------------------------------"); System.out.println(EntityUtils.toString(response.getEntity())); System.out.println("3 ----------------------------------------"); HttpEntity resEntity = response.getEntity(); if (resEntity != null) { System.out.println("Response content length: " + resEntity.getContentLength()); } return "OK"; }