Tôi muốn xây dựng một yêu cầu nhiều phần, với các thông số sau: tên (chuỗi), email (chuỗi) và tệp tải lên (tệp). Tôi đang sử dụng mã Java bên dưới (hoạt động trong Android).Trợ giúp xây dựng một yêu cầu POST với MultipartEntity (câu hỏi mới)
Các httppost.getRequestLine() in
POST http://www.myurl.com/upload HTTP/1.1
Vì vậy, tất cả mọi thứ có vẻ tốt trên trang web của khách hàng, nhưng máy chủ của tôi (Django/Apache) đọc nó như là một yêu cầu GET, không có tham số GET - request.method
sản xuất ' GET ', request.GET.items()
tạo từ điển trống.
Tôi đang làm gì sai? Tôi không biết làm thế nào để thiết lập các thông số multipart một cách chính xác - đang sử dụng phỏng đoán - vì vậy có thể đó là vấn đề.
public void SendMultipartFile() {
Log.e(LOG_TAG, "SendMultipartFile");
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.myurl.com/upload");
File file = new File(Environment.getExternalStorageDirectory(),
"video.3gp");
Log.e(LOG_TAG, "setting up multipart entity");
MultipartEntity mpEntity = new MultipartEntity();
ContentBody cbFile = new FileBody(file);
mpEntity.addPart("fileupload", cbFile);
Log.i("SendLargeFile", "file length = " + file.length());
try {
mpEntity.addPart("name", new StringBody(name));
mpEntity.addPart("email", new StringBody(email));;
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
Log.e(LOG_TAG, "UnsupportedEncodingException");
e1.printStackTrace();
}
httppost.setEntity(mpEntity);
Log.e(LOG_TAG, "executing request " + httppost.getRequestLine());
HttpResponse response;
try {
Log.e(LOG_TAG, "about to execute");
response = httpclient.execute(httppost);
Log.e(LOG_TAG, "executed");
HttpEntity resEntity = response.getEntity();
Log.e(LOG_TAG, response.getStatusLine().toString());
if (resEntity != null) {
System.out.println(EntityUtils.toString(resEntity));
}
if (resEntity != null) {
resEntity.consumeContent();
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Hãy dành một cái nhìn lúc này câu hỏi đã trả lời trước đó http://stackoverflow.com/questions/2017414/post-multipart-request-with-android-sdk – Omie