使用 MultipartEntity 构造 POST 请求
我想构造一个多部分请求,具有以下参数:名称(字符串)、电子邮件(字符串)和文件上传(文件)。我正在使用下面的 Java 代码(在 Android 中工作)。
httppost.getRequestLine() 打印
POST http://www.myurl.com/upload HTTP/1.1
所以一切在客户端站点上看起来都很好,但是我的服务器(Django/Apache)将其读取为 GET 请求,没有 GET 参数 - request.method
生成“GET” , request.GET.items()
生成一个空字典。
我做错了什么?我实际上不知道如何正确设置多部分参数 - 我正在使用猜测 - 所以这可能就是问题所在。
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();
}
}
I'd like to construct a multipart request, with the following parameters: name (string), email (string), and fileupload (file). I'm using the Java code below (working in Android).
The httppost.getRequestLine() prints
POST http://www.myurl.com/upload HTTP/1.1
So everything looks good on the client site, but my server (Django/Apache) reads it as a GET request, with no GET parameters - request.method
produces 'GET', request.GET.items()
produces an empty dictionary.
What am I doing wrong? I don't know actually how to set the multipart parameters correctly - am using guesswork - so it's possible that that's the problem.
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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看来您可能找错地方了。您正在 POSTing,但在 request.GET 中查找数据:
尝试在“request.POST”和“request.FILES”中查找 QueryDict ...
http://docs.djangoproject.com/en/1.6/ref/request-response/#django.http.HttpRequest .文件
Seems likely that you're looking in the wrong place. You're POSTing, but looking for data in request.GET:
Try looking for QueryDict's at "request.POST" and "request.FILES"...
http://docs.djangoproject.com/en/1.6/ref/request-response/#django.http.HttpRequest.FILES
我对 MultipartEntity 请求有同样的问题。我需要将图像上传到服务器。
所以我通过 HttpURLConnection 类做了 MultipartEntity 请求。
我把我的代码放在这里,认为它对你有用。
您需要设置 URL 路径和文件路径。对于这个使用方法 put。
I had same problem with MultipartEntity request. I needed upload image to server.
So I did MultipartEntity request by HttpURLConnection class.
I put here my code, think it can be useful for you.
You need set URL path and file path. For this use method put.