[英]how do i create and post a multipart/mixed http request using Apache HttpComponentst?
我正在使用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/
我試過模仿這個卷曲的帖子,如下所示。
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之后,我得到以下異常(服務器代碼也是在Jetty上運行的Java)。
org.apache.commons.fileupload.FileUploadException:請求被拒絕,因為沒有找到多部分邊界
當我添加一個曲線卷曲
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
我在這里做錯了什么? 任何幫助表示贊賞。
我修改了一下並做了兩件事讓代碼工作。
這是經過修改的片段,以防任何人感興趣。
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=true -Dorg.apache.commons.logging.simplelog.log.org.apache.http=DEBUG
事實證明,每個部分現在都有一個邊界。 更好的是,內容類型和邊界是自動生成的。
內容類型: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";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.