内容长度为空的EntityTemplate [英] Content-length is null for EntityTemplate

查看:341
本文介绍了内容长度为空的EntityTemplate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

适用范围:包含在Android的我使用Apache的HTTP库和尝试通过HttpClient的使用HttpEntity,从EntityTemplate与ContentProducer作出执行HttpPost。
这里的的code

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(...){...}

问题:服务器总是给我411响应code(内容长度为空):

Problem: Server always gives me 411 response code (Content-length is null):

POST / HTTP / 1.1411 180 - -

"POST / HTTP/1.1" 411 180 "-" "-"

这些code工作在某些服务器上。但是,为什么内容长度总是空?
同时,我们也不能手动设置,否则我们都异常产生的原因:

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:

产生的原因:org.apache.http.ProtocolException:Content-Length头已经present

Caused by: org.apache.http.ProtocolException: Content-Length header already present

您的回答非常感谢!

推荐答案

我曾与实体模板相同的问题,因为它不是为一些例如测量内容长度。

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)重写EntityTemplate

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)错误是由非标准请求拦截RequestContent引起的。你可以将其覆盖

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());

这篇关于内容长度为空的EntityTemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆