我有以下代码段,我是使用Apache Camel连接到“ https” REST端点的。问题是,如果运行此命令,我会收到401错误。
from("timer:learnTimer?period=100s")
.to("log:?level=INFO&showBody=true")
.setHeader("currentTime", simple(currentTime))
.setHeader(Exchange.CONTENT_TYPE,constant("application/json"))
.setHeader(Exchange.HTTP_METHOD, constant("GET"))
.setHeader(Exchange.HTTP_URI, simple("https://xxxxxx/api/siem/offenses?filter=status%20%3D%20%22OPEN%22%20and%20start_time%20%3E%201543647979000?&authMethod=Basic&authUsername=xxxxx&authPassword=xxxxx"))
.to("https://xxxxxxx/api/siem/offenses?filter=status%20%3D%20%22OPEN%22%20and%20start_time%20%3E%201543647979000?&authMethod=Basic&authUsername=xxxx&authPassword=xxxx").convertBodyTo(String.class)
.to("log:?level=INFO&showBody=true");
我收到的错误是:
org.apache.camel.http.common.HttpOperationFailedException:HTTP操作无法调用状态为401的https://xx.xx.xx.xx/api/siem/offenses?filter=status+%3D+%22OPEN%22+and+start_time+%3E+1543647979000%3F 在org.apache.camel.component.http.HttpProducer.populateHttpOperationFailedException(HttpProducer.java:243) 在org.apache.camel.component.http.HttpProducer.process(HttpProducer.java:165) 在org.apache.camel.util.AsyncProcessorConverterHelper $ ProcessorToAsyncProcessorBridge.process(AsyncProcessorConverterHelper.java:61) 在org.apache.camel.processor.SendProcessor.process(SendProcessor.java:148) 在org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:548) 在org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201) 在org.apache.camel.processor.Pipeline.process(Pipeline.java:138) 在org.apache.camel.processor.Pipeline.process(Pipeline.java:101) 在org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201) 在org.apache.camel.component.timer.TimerConsumer.sendTimerExchange(TimerConsumer.java:197) 在org.apache.camel.component.timer.TimerConsumer $ 1.run(TimerConsumer.java:79) 在java.util.TimerThread.mainLoop(Timer.java:555) 在java.util.TimerThread.run(Timer.java:505) 15:16 |警告| CamelLogger.java 213 |错误处理交换。交换[ID-zabbixproxy-node2-1544019394005-0-1]。由以下原因引起:[org.apache.camel.http.common.HttpOperationFailedException-HTTP操作无法调用状态代码为401的https://xx.xx.xx.xx/api/siem/offenses?filter=status+%3D+%22OPEN%22+and+start_time+%3E+1543647979000%3F org.apache.camel.http.common.HttpOperationFailedException:HTTP操作无法调用状态代码为401的https://10.96.40.66/api/siem/offenses?filter=status+%3D+%22OPEN%22+and+start_time+%3E+1543647979000%3F 在org.apache.camel.component.http.HttpProducer.populateHttpOperationFailedException(HttpProducer.java:243) 在org.apache.camel.component.http.HttpProducer.process(HttpProducer.java:165) 在org.apache.camel.util.AsyncProcessorConverterHelper $ ProcessorToAsyncProcessorBridge.process(AsyncProcessorConverterHelper.java:61) 在org.apache.camel.processor.SendProcessor.process(SendProcessor.java:148)
答案 0 :(得分:0)
确定要在进行其余呼叫之前设置这些标头吗? IN消息中不必要的请求标头可能会导致某些问题。
Exchange exchange = ExchangeBuilder.anExchange(camelContext)
.withHeader("").withProperty("")
.withPattern(ExchangePattern...)
.withHeader(Exchange.HTTP_METHOD, HttpMethod.GET)
.build();
producer.send("the end point to rest",exchange);
//生产者是ProducerTemaplte
在上面的代码中,您可以设置ExchangePattern以及必需的标头和属性(如果仅需要)。
希望这会有所帮助。