Content-length is null for EntityTemplate

2019-08-23 01:08发布

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!

1条回答
狗以群分
2楼-- · 2019-08-23 01:46

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

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) Error is caused by standart Request interceptor RequestContent. You can override it

    // 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());
查看更多
登录 后发表回答