2011-07-28 20 views
0

対象範囲: Androidに含まれているhttp apacheライブラリを使用しており、HttpPostをHttpClient経由でHttpEntityで実行しようとしています(ContentProducerでEntityTemplateから作成)。EntityTemplateのコンテンツ長がnullの場合

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

問題:: Serverは常に(コンテンツ長がnullである)私に411応答コードを示します:

"POST/HTTP/1.1" 411 180 "ここ はコードです - "" - "

これらのコードは一部のサーバーで動作します。しかし、なぜコンテンツの長さが常にnullですか? また、我々は、我々はによって引き起こされる例外持っているそれ以外の場合は、手動で設定することはできません。

によって引き起こさ

:org.apache.http.ProtocolException:あなたのためのContent-Lengthヘッダが既に存在

感謝を答え!

+0

www.google.comのようないくつかの標準的なサーバーにアクセスし、あなたのコードが正しい場合は出力としていくつかのHTMLテキストを取得する必要があります。 – Naresh

+0

Google.comは私に405を与えます(とにかく投稿を拒否するので、コンテンツ長のチェックには使用できません)。私は別のサーバーを試しました、彼らはすべて私に411を与える。ありがとう、それはアイデアだった!しかし、まだ幸運... –

+0

あなたはHttpPostの代わりにHTTpGetメソッドを使用する必要があります – Naresh

答えて

1

エンティティテンプレートと同じ問題がありました。コンテンツの長さは一部のインスタンスでは測定されませんでした。

私のための2つの回避策があります。

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()); 
関連する問題