我正在尝试将一些XML写入服务器,但它的要点是无论我做什么,HttpPut根本不会在Http体内放任何东西。服务器总是回来说身体不见了,通过Wireshark看着它,什么都没有!这是我用来设置和运行请求的代码:
HttpPut putRequest = new HttpPut(urlString]);
StringEntity stringEntity = new StringEntity(xmlString, HTTP.ISO_8859_1);
stringEntity.setContentType("text/xml");
putRequest.setEntity(stringEntity);
putRequest.addHeader("Host", formatUrlForHostHeader(broadsoftUrl));
putRequest.addHeader("Authorization", authorizationString);
putRequest.addHeader("Content-Type", "text/xml");
putRequest.addHeader("Accept", "text/xml");
response = httpClient.execute(putRequest);
我不确定这里还包括什么。我在4.2和4.0.3上尝试过。此代码在AsyncTask的doInBackground中运行。我得到的响应代码是409 Conflict,正文是服务器特定于应用程序的消息,告诉我身体缺失。我确认Wireshark缺少它。
修改
一个有趣的说明是我在桌面上独立运行相同的代码,并且它有效。那么,HttpClient或系统的Android版本有什么用吗?我也尝试了一些不同的API级别,只是为了检查。
有什么想法吗?
谢谢!
答案 0 :(得分:0)
好吧,所以解决方案就是放弃HttpPut和所有这些,并使用HttpURLConnection。以下是我们最终如何做到这一点:
URL url = new URL(theUrl);
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setRequestProperty("Host", formatUrlForHostHeader(broadsoftUrl));
httpCon.setRequestProperty("Authorization", authorizationString);
httpCon.setRequestProperty("Content-Type", "text/xml; charset=ISO_8859_1");
httpCon.setRequestProperty("Accept", "text/xml");
httpCon.setDoInput(true);
httpCon.setDoOutput(true);
httpCon.setRequestMethod("PUT");
OutputStreamWriter out = new OutputStreamWriter(httpCon.getOutputStream(), "ISO_8859_1");
out.write(xmlData);
out.close();
if(httpCon.getErrorStream() == null) {
return "";
} else {
return "ERROR";
}
我们不需要从PUT请求中获取响应,但是通过查看错误流是否存在来检查它是否失败。如果你想得到答案,你会做这样的事情:
StringWriter writer = new StringWriter();
IOUtils.copy(httpCon.getInputStream(), writer, encoding);
String responseString = writer.toString();
当然,您必须在您的应用中包含Apache的IOTools。
答案 1 :(得分:-1)
409冲突通常是 Edit Conflict错误,通常与wiki有关,但可能与请求有任何类型的冲突。
http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
您尝试发布哪种类型的数据,主机是否有可能无法更改的现有数据?