Camel http4组件411 Bad Content-Length

时间:2013-01-16 18:45:49

标签: apache-camel http-status-code-411

路线:

  from("direct:start")
  .setProperty(Exchange.CHARSET_NAME, constant("iso-8859-1"))
  .process(new Processor() {            
        @Override
        public void process(Exchange exchange) throws Exception {
          Message m = exchange.getOut();
          m.setBody(exchange.getIn().getBody());
          m.setHeader(Exchange.HTTP_METHOD, HttpMethods.POST);
          m.setHeader(Exchange.CONTENT_ENCODING, "gzip" );
          m.setHeader(Exchange.CONTENT_LENGTH, m.getBody(byte[].class).length );
          m.setHeader(HttpHeaders.CONTENT_TYPE, "application/xml");
          m.setHeader(Exchange.HTTP_CHARACTER_ENCODING, "iso-8859-1");
          m.setHeader(HttpHeaders.ACCEPT_ENCODING, "gzip, deflate");        
        }
    })
  .marshal().gzip()
  .to("http4://remote.com/path")
  .unmarshal().gzip();

我发送的内容:

String body = "<?xmlversion=\"1.0\"encoding=\"ISO-8859-1\"?><theXml></theXml>";
producer.sendBody(body);

我正在

  

HTTP操作无法使用statusCode:411

调用http://remote.com/path

这条路线有什么遗漏/错误?

修改

正确的路线是

  from("direct:start")
  .process(new Processor() {            
        @Override
        public void process(Exchange exchange) throws Exception {
          Message m = exchange.getOut();
          m.setBody(exchange.getIn().getBody());
          m.setHeader(Exchange.HTTP_METHOD, HttpMethods.POST);
          m.setHeader(Exchange.CONTENT_ENCODING, "gzip" );
          m.setHeader(Exchange.CONTENT_TYPE, "application/xml");        
        }
    })
  // http4 takes care of compressing/decompressing gzip
  .to("http4://remote.com/path")

但是现在我还有另外一个问题:远程服务器没有处理“Transfer-Encoding:Chuncked”这似乎是camel-http4的默认方式。 我无法弄清楚如何关闭 Chunked

请参阅下一个问题How to turn off “Transfer-Encoding Chuncked” in Camel-http4?

1 个答案:

答案 0 :(得分:1)

您正在从未编码数据的长度设置内容长度。它应该是传输数据的长度。请参阅此SO问题: content-length when using http compression

顺便问一下,你真的需要用数据格式进行gzip吗? 骆驼发送GZIPed数据时有一个单元测试。

https://svn.apache.org/repos/asf/camel/trunk/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpCompressionTest.java