【发布时间】:2010-06-21 23:05:59
【问题描述】:
编辑:
我正在尝试在 Android 中将 xml 文件作为 post 请求发送。
服务器接受文本/xml。我尝试创建一个 MultipartEntity,它的内容类型为 multipart/form-data。
HttpClient httpClient = new DefaultHttpClient();
/* New Post Request */
HttpPost postRequest = new HttpPost(url);
byte[] data = IOUtils.toByteArray(payload);
/* Body of the Request */
InputStreamBody isb = new InputStreamBody(new ByteArrayInputStream(data), "uploadedFile");
MultipartEntity multipartContent = new MultipartEntity();
multipartContent.addPart("uploadedFile", isb);
/* Set the Body of the Request */
postRequest.setEntity(multipartContent);
/* Set Authorization Header */
postRequest.setHeader("Authorization", authHeader);
HttpResponse response = httpClient.execute(postRequest);
InputStream content = response.getEntity().getContent();
return content;
但是,我收到一条错误消息,提示无法使用该内容类型。
The server refused this request because the request entity is in a format not supported by the requested resource for the requested method (Cannot consume content type).
如何更改请求的内容类型?
编辑:
【问题讨论】:
-
你想把它改成什么?现在是什么,为什么服务器不支持?
-
如果您发送 xml,您使用 MultipartEntity 是否有某些特定原因?
-
@Lauri。我想我也会发送一个 StringBody 。有没有更简单的方法只发送 XML 文件?
-
您希望服务器接收 XML 文件还是只接收 XML 内容?
-
告诉编写服务器的人,他们应该使用
PUT,而不是POST,来提交text/xml内容。
标签: java android apache httpclient http-post