我正在尝试使用 发布数据multipart/form-data
,但它不适用于 httpPost 方法。我必须从 API 以 json 字符串的形式发送数据,但是如果我删除content-type
标头,服务器会给出错误的响应,响应说没有编辑以下数据的权限,我必须使用 API 从 API 发送 2 个参数POST
,
下面是我的代码:
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;
}
有什么我做错了,或任何其他方法。