使用 jax-ws 调用位于负载均衡器上的 Web 服务返回的 http 状态为 302,但是当我使用 SoapUI 时它工作正常 [英] Calling web service that sits on a load balancer with jax-ws returns at http status of 302, but when I use SoapUI it works fine

查看:10
本文介绍了使用 jax-ws 调用位于负载均衡器上的 Web 服务返回的 http 状态为 302,但是当我使用 SoapUI 时它工作正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用 jax-ws 调用位于负载均衡器上的 Web 服务时,它返回

When I call a web service that sits on a load balancer with jax-ws, it returns

服务器发送 HTTP 状态码 302:临时移动

The server sent HTTP status code 302: Moved Temporarily

然后失败,但是当我使用 SoapUI 时它工作正常.

and then fails, but when I use SoapUI it works fine.

有没有办法配置服务来正确处理这个问题?

Is there a way that I can configure the service to handle this correctly?

我使用 wsimport 生成了 webservice 代码并进行调用

I generated the webservice code using wsimport and make the call as such

NotificationWebService service = new NotificationWebService(wsdlLocation, qName);
NotificationWebServiceSoap serviceSoap = service.getNotificationWebServiceSoap();
String xmlString = serviceSoap.getRSAPublicKeyXMLString();

我被困住了,我无法在任何地方找到解决方案,因此我们将不胜感激.

I'm stuck and I haven't been able to find a solution anywhere so any help would be appreciated.

推荐答案

所以经过大量调查后,我终于弄清楚了问题所在.这完全取决于从 http 重定向到 https.从我在网上找到的文章(不再记得网址了),wsdl2javawsimport 存根生成器用来进行网络服务通信的库不出于安全原因,不允许 http 到 https 重定向.

So after lots of investigation I finally figured out what the problem was. It was all down to redirecting from http to https. From articles I found on the web (can't remember the urls anymore), is that the libraries that the wsdl2java and wsimport stub generators use to do the webservice communication don't allow a http to https redirect follow due to security reasons.

所以即使我从 https wsdl 位置生成存根,即.https://wsdl.location.com?wsdl,当我运行代码进行网络服务调用时,它试图调用 http://wsdl.location.com 这导致重定向请求到 https://wsdl.location.com,但 http 库不允许.所以它只是将 302 http 代码作为异常转发.

So even though I was generating the stubs from a https wsdl location ie. https://wsdl.location.com?wsdl, when I ran the code to do a webservice call, it was trying to make the call to http://wsdl.location.com which resulted in a redirect request to https://wsdl.location.com, But the http library does not allow that. So it just forwards the 302 http code up as an exception.

此外,还有两个网络服务网址.一个是测试服务,它是普通的 http url,然后是一个通过 https 的生产服务器.因此,为了解决这个问题,我所做的就是通过使用 BindingProvider 类动态配置服务以使用我指定的端点(现在是 https 地址).这也允许我根据正在调用的环境动态指定使用测试 url 或生产 ie.

Also, there are two web-service urls. One is a testing service which is normal http url and then there is a production server which is over https. So to work around this is all I did is configure the service on the fly to use the end-point I have specified (which is now the https address) by using the BindingProvider class. This also allows me to specify on the fly depending on which environment that is making to call, to use the test url or the production one ie.

NotificationWebService service = new NotificationWebService();
NotificationWebServiceSoap serviceSoap = service.getNotificationWebServiceSoap();

BindingProvider bindingProvider = (BindingProvider) serviceSoap;
bindingProvider.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "https://wsdl.location.com");

String xmlString = serviceSoap.getRSAPublicKeyXMLString();

简而言之,如果有人遇到这个问题.确保您需要指向的端点是否在 https 上,您正在进行直接 https 调用,而不是要求 https 重定向的 http 调用.您可以在调试时通过检查 serviceSoap 来检查这一点.它会告诉它正在向哪个 url 发出呼叫.

So in short, if anyone encounters this problem. Make sure if the end point you need point to is on https, that you are making a direct https call and not a http call that will ask for an https redirect. You can check this by examining the serviceSoap while debugging. It will say to which url it is making the call.

我没有研究或使用配置 wsdl2javawsimport 的选项来生成存根并强制存根使用 https 调用,以便我可以为不同环境配置多个url端点.

I didn't look into or go with the option to configure wsdl2java and wsimport to generate the stubs and force the stubs to use the https call so that I could configure multiple url end-points for different environments.

这篇关于使用 jax-ws 调用位于负载均衡器上的 Web 服务返回的 http 状态为 302,但是当我使用 SoapUI 时它工作正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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