【发布时间】:2011-07-28 10:53:49
【问题描述】:
范围:我正在使用 Android 中包含的 http apache 库,并尝试通过 HttpClient 和 HttpEntity 执行 HttpPost,由 EntityTemplate 和 ContentProducer 制成。 这是代码:
HttpClient httpClient = new CustomHttpsClient().getNewHttpClient();
HttpPost httpPost = new HttpPost("APP_URI");
HttpEntity postEntity;
HttpEntity getEntity;
ContentProducer contentProducer = new ContentProducer() {
@Override
public void writeTo(OutputStream outstream) throws IOException {
// TODO Auto-generated method stub
Writer writer = new OutputStreamWriter(outstream, "UTF-8");
writer.write("<req version=\"1.0\" lang=\"RU\">");
writer.write("<act>o_addcard</act>");
writer.write("</req>");
writer.flush();
}
};
postEntity = new EntityTemplate(contentProducer);
httpPost.setEntity(postEntity);
String responseEntity;
try {
HttpResponse httpResponse = httpClient.execute(httpPost);
...} catch(...){...}
问题:服务器总是给我411响应码(内容长度为空):
“POST / HTTP/1.1”411 180“-”“-”
这些代码适用于某些服务器。但是为什么 content-length 总是为空呢? 另外我们不能手动设置,否则会出现异常原因:
原因:org.apache.http.ProtocolException:Content-Length 标头已存在
非常感谢您的回答!
【问题讨论】:
-
尝试访问一些标准服务器,如 www.google.com,看看你的内容长度是否为空,如果你的代码正确,你应该得到一些 html 文本作为输出。
-
Google.com 给了我 405(无论如何它拒绝发布,所以我们不能用它来检查内容长度)。我尝试了另一台服务器,它们都给了我 411。谢谢这是一个想法!但仍然没有运气......
-
也许你应该使用 HTTpGet 方法而不是 HttpPost
-
没有区别,411 与另一个受人尊敬的服务器的 post 方法。
-
解决方案是使用 StringEntity 代替。我应该在这里发布我的工作代码(使用 StringEntity)作为答案吗?