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

14,470

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.

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.

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

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.

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.

Share:
14,470
Clayton
Author by

Clayton

Updated on September 05, 2022

Comments

  • Clayton
    Clayton over 1 year

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

    The server sent HTTP status code 302: Moved Temporarily

    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?

    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.

  • Clayton
    Clayton over 11 years
    I'm just using the code that was generated by the wsimport command, so it will be using the default transports that come with it. Thanks, I will look in to the other two frame works you have suggested.
  • Clayton
    Clayton over 11 years
    Hi, I used axis to generate the the client and I still get the same error. org.apache.axis2.AxisFault: Transport error: 302 Error: Moved Temporarily. But when I use SoapUI it works fine. So I think its a deeper issue. So I'm still looking for a way to allow to follow 302 redirects.
  • Clayton
    Clayton over 11 years
    ok, I have found out what the exact problem is but not what the solution is. The wsdl url that I generate the stubs from is over http, but when I use the stubs, it makes the call over http and its trying to redirect to https. Apparently in Java from what I have read, that is a no no. So I need to find a way when I instantiate the call NotificationWebServiceSoap serviceSoap = service.getNotificationWebServiceSoap(); it must make sure it does it over https.
  • Mikkel Løkke
    Mikkel Løkke over 11 years
    I'm not really able to help you with Axis2 myself, as I use cxf, but according to this page: axis.apache.org/axis2/java/core/docs/http-transport.html you need to specify (through configuration) that you want to use https, Maybe that helps?
  • Tushar Patel
    Tushar Patel almost 9 years
    Does error occur vice versa as well? We have https call which call http url. We get 302 error. Please provide suggestion.