0

路线 :

  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 操作调用http://remote.com/path失败,状态码:411

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

编辑

正确的路线是

  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

请参阅下一个问题如何在 Camel-http4 中关闭“Transfer-Encoding Chuncked”?

4

1 回答 1

1

您正在根据未编码数据的长度设置内容长度。它可能应该是传输数据的长度。请参阅此 SO 问题: 使用 http 压缩时的内容长度

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

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

于 2013-01-17T08:33:57.180 回答