使用 ProducerTemplate 使用 Camel 进行代理身份验证 [英] Proxy authentication with Camel using ProducerTemplate

查看:58
本文介绍了使用 ProducerTemplate 使用 Camel 进行代理身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 Camel 并在 ServiceMix 服务器上运行的项目,但我似乎无法让它访问外部 Web 服务,我怀疑这是因为我无法正确设置代理身份验证.

I have a project that's using Camel and running on a ServiceMix server, but I can't seem to get it to access external web services, and I suspect it's because I can't set the proxy authentication properly.

Exchange exchange = producerTemplate.request(url, new Processor() {
    public void process(Exchange exchange) throws Exception {
        exchange.getIn().setHeader(Exchange.HTTP_METHOD, "POST");
        exchange.getIn().setHeader(Exchange.CONTENT_TYPE, "application/json");
    }
});
response = exchange.getOut().getBody(String.class);

如果我在最后一行设置断点,我会在交换对象中看到 ConnectionTimedOutException 并且响应为空.

If I put a breakpoint on the last line, I see a ConnectionTimedOutException in the exchange object and the response is null.

我尝试了多种方式设置代理.

I tried setting the proxies in a multitude of ways.

1) 我尝试在实现CamelContextAware的类中设置代理设置:

1) I tried setting the proxy settings in a class that implements CamelContextAware:

camelContext.getProperties().put("http.proxyHost", "...");
camelContext.getProperties().put("http.proxyPort", "8080");
camelContext.getProperties().put("http.proxyUser", "...");
camelContext.getProperties().put("http.proxyPassword", "...");
camelContext.getProperties().put("http.proxySet", "true");

这在独立模式下工作,但是当我在 ServiceMix 中部署代码时,camelContext 对象为空.

This works in standalone mode, but when I deploy the code in ServiceMix, the camelContext object is null.

2)我尝试在ServiceMix的etc/system.properties文件中设置代理设置.

2) I tried setting the proxy settings in the etc/system.properties file of ServiceMix.

3) 我尝试在 camel-context.xml 中使用 http-conf:conduit,如下所示:

3) I tried using http-conf:conduit in the camel-context.xml like this:

<http-conf:conduit name="*.http-conduit">
    <http-conf:client ProxyServer="..." ProxyServerPort="8080" />
    <http-conf:proxyAuthorization>
        <conf-sec:UserName>...</conf-sec:UserName>
        <conf-sec:Password>...</conf-sec:Password>
    </http-conf:proxyAuthorization>
</http-conf:conduit>

但是,我认为这只有在我使用 cxf 客户端时才有效.

However, I think that'd only work if I used a cxf client.

没有任何效果,我需要它在部署到 ServiceMix 时工作.任何帮助将不胜感激.

Nothing worked, and I need it to work while deployed on the ServiceMix. Any help would be greatly appreciated.

谢谢.

推荐答案

试试这个代码:

HTTPConduit conduit = (HTTPConduit)outMessage.getExchange().getConduit(outMessage);
HTTPClientPolicy policy = conduit.getClient();
policy.setProxyServer(PROXY_IP);
policy.setProxyServerPort(PROXY_PORT);
conduit.setClient(policy);

这篇关于使用 ProducerTemplate 使用 Camel 进行代理身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆