EntityTemplate的Content-length为null

时间:2011-07-28 10:53:49

标签: android apache http

范围:我正在使用Android中包含的http apache库,并尝试通过HttpClient使用HttpEntity执行HttpPost,HttpEntity由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响应代码(Content-length为null):

  

“POST / HTTP / 1.1”411 180“ - ”“ - ”

这些代码适用于某些服务器。但为什么内容长度始终为空? 我们也不能手动设置它,否则我们有异常引起:

  

引起:org.apache.http.ProtocolException:Content-Length标头已存在

非常感谢你的回答!

1 个答案:

答案 0 :(得分:1)

我对实体模板有同样的问题,因为内容长度未针对某些实例进行测量。

我有两种可行的解决方法:

1)覆盖EntityTemplate

class EntityTemplateSpike extends EntityTemplate{

    public EntityTemplateSpike(ContentProducer contentproducer) {
        super(contentproducer);
    }

    @Override
    public long getContentLength() {
        return // Paste here your ContentLength value
    }

}
postEntity = new EntityTemplateSpike(contentProducer);

2)错误是由标准的请求拦截器RequestContent引起的。你可以覆盖它

    // This class is a copy of standart RequestContent class except for Exception
        // is not being thrown if ContentLegth is already set.
        private class RequestContentSpike implements HttpRequestInterceptor{
                     public RequestContentSpike() {
                         super();
                     }

                     public void  process(final HttpRequest request, final HttpContext context)  throws HttpException, IOException {
                         if (request == null) {
                             throw new IllegalArgumentException("HTTP request may not be null");
                         }

                         if (request instanceof HttpEntityEnclosingRequest) {

                             if (request.containsHeader(HTTP.TRANSFER_ENCODING)) {
                                 throw new ProtocolException("Transfer-encoding header already present");
                             }

                             ProtocolVersion ver = request.getRequestLine().getProtocolVersion();
                             HttpEntity entity = ((HttpEntityEnclosingRequest)request).getEntity();

                             if (entity == null) {
                                 request.addHeader(HTTP.CONTENT_LEN, "0");
                                 return;
                             }

                             if (entity.isChunked() || entity.getContentLength() < 0) {
                                 if (ver.lessEquals(HttpVersion.HTTP_1_0)) {
                                     throw new ProtocolException( "Chunked transfer encoding not allowed for " + ver);
                                 }
                                 request.addHeader(HTTP.TRANSFER_ENCODING, HTTP.CHUNK_CODING);
                             } else {
                                 request.addHeader(HTTP.CONTENT_LEN, Long.toString(entity.getContentLength()));
                             }


                             if (entity.getContentType() != null && !request.containsHeader(HTTP.CONTENT_TYPE )) {
                                 request.addHeader(entity.getContentType()); 
                             }


                             if (entity.getContentEncoding() != null && !request.containsHeader(HTTP.CONTENT_ENCODING)) {
                                request.addHeader(entity.getContentEncoding()); 
                            }
                        }
                    }
        }
    ((CustomHttpsClient)mClient).removeRequestInterceptorByClass(RequestContent.class);
  ((CustomHttpsClient)mClient).addRequestInterceptor(new RequestContentSpike());