我正在尝试使用multipart/form-data
发布数据,但它无法使用httpPost方法。我必须从API发送数据作为json字符串,但是如果我删除了content-type
标题,它会给出来自服务器的错误响应,响应说不具备编辑后续数据的权限,
我必须使用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;
}
我做错了什么,或者其他任何方法。