【发布时间】:2013-03-28 09:26:18
【问题描述】:
我正在尝试使用multipart/form-data 发布数据,但它不适用于 httpPost 方法。我必须从 API 以 json 字符串的形式发送数据,但是如果我删除 content-type 标头,服务器会给出错误的响应,响应说没有编辑以下数据的权限,
我必须使用 POST 从 API 发送 2 个参数,
下面是我的代码:
public String webPostMultiForm(List<NameValuePair> params) {
String postUrl = webServiceUrl;
httpPost = new HttpPost(postUrl);
try {
httpPost.setEntity(new UrlEncodedFormEntity(params));
httpPost.addHeader("Cookie",appStatus.getSharedStringValue(appStatus.AUTH_KEY));
httpPost.addHeader("Content-Type","multipart/form-data; boundary=assdsfdffafasf");
httpPost.addHeader("Content-Disposition", "form-data; name= args");
//httpPost.setHeader("Transfer-Encoding","chunked");
} catch (UnsupportedEncodingException uee) {
Log.e(TAG, uee.getMessage());
}
Log.e(TAG,"WebGetURL: " + postUrl);
try {
response = httpClient.execute(httpPost);
} catch (Exception e) {
if (e.getMessage() != null) {
Log.e(TAG, "httpClient.execute(httpPost) Exception: " + e.getMessage());
} else {
Log.e(TAG, "httpClient.execute(httpPost) Exception: " + e.getClass().toString());
}
}
try {
strResponse = EntityUtils.toString(response.getEntity());
} catch (Exception e) {
if (e.getMessage() != null) {
Log.e(TAG, e.getMessage());
} else {
Log.e(TAG, e.getClass().toString());
}
}
return strResponse;
}
有什么我做错了,或者任何其他方法。
【问题讨论】:
标签: java android http-headers mime-types