java 使用 jax-ws 调用位于负载均衡器上的 Web 服务返回的 http 状态为 302,但是当我使用 SoapUI 时它工作正常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13702022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 13:42:08  来源:igfitidea点击:

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

javaweb-servicessoapjax-wsload-balancing

提问by Clayton

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

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

The server sent HTTP status code 302: Moved Temporarily

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

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

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

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

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

I generated the webservice code using wsimportand make the call as such

我使用生成的网络服务代码wsimport并进行调用

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.

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

回答by Clayton

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 wsdl2javaand wsimportstub generators use to do the webservice communication don't allow a http to https redirect follow due to security reasons.

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

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.comwhich 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.

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

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 BindingProviderclass. 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.

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

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();

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 serviceSoapwhile debugging. It will say to which url it is making the call.

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

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

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

回答by Mikkel L?kke

Yes. You didn't say what you're using for the transport, but if it's something like HttpClient you need to set it up to follow redirects. You may need to fiddle with the auto-generated code, or alternatively, perhaps try a higher level abstraction, like Spring Web Services or CXF.

是的。您没有说明您用于传输的内容,但如果它是 HttpClient 之类的东西,您需要将其设置为遵循重定向。您可能需要处理自动生成的代码,或者尝试更高级别的抽象,例如 Spring Web Services 或 CXF。

回答by Wankhade N. D.

Reason is the redirection issue on web service call control gets transfered from HTTP to HTTPS. Edit your WSDL file you will find the below code:

原因是 Web 服务调用控制上的重定向问题从 HTTP 转移到 HTTPS。编辑您的 WSDL 文件,您将找到以下代码:

original code:

原始代码:

soap:address location="http://www.google.com/Services/Services_xyz.asmx"

changed code:

更改代码:

soap:address location="https://www.google.com/Services/Services_xyz.asmx"

Just change the location attribute HTTP to HTTPS and generate the new stub using the changed WSDL file location.

只需将位置属性 HTTP 更改为 HTTPS 并使用更改后的 WSDL 文件位置生成新的存根。