我試圖上傳一個文件,以及一些變量到一個web服務。從我在網上閱讀的內容中,MultipartEntityBuilder將成爲這項工作的工具。但是,當我以描述的方式使用它時,post變量(包括文本和文件)似乎不會與查詢一起發送。接收端反應就像沒有附加變量後綴一樣。MultipartEntityBuilder不發送帖子變量
我已經嘗試通過用下面的代碼文件指向的URL測試理論:
<?php
$data = "";
foreach ($_POST as $key => $value) {
$data = $data . "Field ".htmlspecialchars($key)." is ".htmlspecialchars($value)."<br>";
}
echo $data;
?>
,並且調用返回沒有的數據(並沒有當我使用過賬等方法返回的數據
是否有任何想法,可能是什麼問題?
The code in question:
String url = "http://aktivthospital.dk.web1.aktivthospital.dk/index.php";
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("Content-Type", "multipart/form-data");
MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
entityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
ContentType contentType = ContentType.create(HTTP.PLAIN_TEXT_TYPE, HTTP.UTF_8);
entityBuilder.addPart("option", new StringBody("com_redtournament", contentType));
entityBuilder.addPart("task", new StringBody("competition.apiServiceUploadImage", contentType));
entityBuilder.addPart("username", new StringBody(_username, contentType));
entityBuilder.addPart("password", new StringBody(_password, contentType));
entityBuilder.addPart("comp_id", new StringBody(String.valueOf(_competitionId), contentType));
entityBuilder.addPart("img_title", new StringBody(_title, contentType));
File file = new File(_imagepath);
entityBuilder.addPart("img_file",new FileBody(file));
httpPost.setEntity(entityBuilder.build());
HttpResponse response = httpClient.execute(httpPost);
你有很多零件。你不想使用1部分的URL編碼對,1的文件? – njzk2
我需要發送所有的東西作爲一個電話。否則,Web服務將不知道如何處理該文件。是否有可能分裂它,並仍然是一次呼叫? – Rubberduck
不是我在說什麼。你正在製作一個包含很多零件的多部件。通常,有2個部分,而不是7.1與參數名稱值對,一個與文件。 – njzk2