8

如何使用此路由转储使用 Apache Camel HTTP 组件发送的 HTTP 正文和标头:

   from('direct:abc').
   setHeader(Exchange.HTTP_URI, constant("${config.gnutch.solr.coreUrl}/select")).
   setHeader(Exchange.HTTP_QUERY, constant("q=${q}&wt=xml")).
   setHeader(Exchange.CONTENT_TYPE, constant('application/xml')).
   setHeader(Exchange.HTTP_METHOD, constant('GET')).
   setBody(constant(null)).
   to("http://null")

这是 groovy 中的 Camel DSL 代码。那可能吗?

4

4 回答 4

12

你有没有尝试过类似的东西

from("direct:abc")
 .to("http://domain.com/")
 .to("log:DEBUG?showBody=true&showHeaders=true")

HTTP 组件文档还建议您可以HttpServletRequest从交换中提取,例如,

HttpServletRequest request = exchange.getIn().getBody(HttpServletRequest.class);

然后,您可以选择这样做,

from("direct:abc").to("http://domain.com").process(new Processor() {
    public void process(Exchange exchange) throws Exception {
        HttpServletRequest request = exchange.getIn().getBody(HttpServletRequest.class);
        // Log request parameters
    }
});
于 2013-11-14T12:15:01.997 回答
8

这将有所帮助,在日志消息中使用它:

${标题}

或者

${in.headers}

这将打印任何传入的标题。

在这里结帐:http: //camel.apache.org/simple.html

例子 :

于 2016-04-09T21:37:11.083 回答
4

尝试使用 HttpClient 记录器之一记录标头和内容。

日志记录实践(在这种情况下为 3.x 版)中描述了它。

我正在使用带有名称的记录器:

  • httpclient.wire
  • org.apache.commons.httpclient.HttpConnection

这给了我这样的输出:

o.a.c.httpclient.HttpConnection - Open connection to 0.0.0.0:12454
httpclient.wire.header - >> "POST /some/path HTTP/1.1[\r][\n]"
httpclient.wire.header - >> "breadcrumbId: ID-localhost-55077[\r][\n]"
httpclient.wire.header - >> "path: http://0.0.0.0:65432/some/other/path[\r][\n]"
httpclient.wire.header - >> "User-Agent: Jakarta Commons-HttpClient/3.1[\r][\n]"
httpclient.wire.header - >> "Host: 0.0.0.0:12454[\r][\n]"
httpclient.wire.header - >> "Content-Length: 117[\r][\n]"
httpclient.wire.header - >> "[\r][\n]"
httpclient.wire.content - >> "{"a":"afeaafe","b":{"c":"53413"},"d":{"e":"vsegefawawewr"}}"
httpclient.wire.header - << "HTTP/1.1 200 OK[\r][\n]"
httpclient.wire.header - << "HTTP/1.1 200 OK[\r][\n]"
httpclient.wire.header - << "Date: Fri, 08 Apr 2016 07:24:24 GMT[\r][\n]"
httpclient.wire.header - << "Content-Type: application/octet-stream[\r][\n]"
httpclient.wire.header - << "Date: Fri, 08 Apr 2016 07:24:24 GMT[\r][\n]"
httpclient.wire.header - << "Content-Length: 7[\r][\n]"
httpclient.wire.header - << "Server: Jetty(9.2.10.v20150310)[\r][\n]"
httpclient.wire.header - << "[\r][\n]"
httpclient.wire.content - << "Success"
o.a.c.httpclient.HttpConnection - Releasing connection back to connection manager.
于 2016-04-08T07:41:37.620 回答
0

使用 Spring Boot 2.4 中的 Camel 3.11 和 apache httpclient 4.5,它需要放置

logging.level.org.apache.http.wire=DEBUG

为我进入 application.properties。

于 2021-12-10T13:23:52.783 回答