路线:
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);
I am getting
使用statusCode: 411调用http://remote.com/path HTTP操作失败
这条路由有什么缺失/错误?
编辑
正确的路径是
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")
但现在我有另一个问题:远程服务器不处理"传输编码:分块",这似乎是默认的方式骆驼-http4做到这一点。我不知道怎么把Chunked关闭。
参见下一个问题如何在Camel-http4中关闭"传输编码块"?
根据未编码数据的长度设置内容长度。它应该是传输数据的长度。参考这个SO问题:使用HTTP压缩时的内容长度
顺便问一下,你真的需要用数据格式进行gzip吗?有一个驼峰发送gzip数据的单元测试。
https://svn.apache.org/repos/asf/camel/trunk/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpCompressionTest.java