Scope: I'm using http apache libraries included in Android and trying to execute HttpPost via HttpClient with HttpEntity, made from EntityTemplate with ContentProducer. Here is the code:
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(...){...}
Problem: Server always gives me 411 response code (Content-length is null):
"POST / HTTP/1.1" 411 180 "-" "-"
These code works on some servers. But why content-length is always null? Also we can't set it manually, otherwise we have exception Caused by:
Caused by: org.apache.http.ProtocolException: Content-Length header already present
Many thanks for your answers!
I had the same issue with Entity template, as it is content Length was not measured for some instance.
There are two possible workarounds for me:
1) Overriding EntityTemplate
2) Error is caused by standart Request interceptor RequestContent. You can override it