javasoapwebservice-clientsoapheader

How to pass multiple parameters through soap header in java?


I'm new to SOAP and I am trying to get some data from the SOAP Web service. there is only one method that I can call directly from the WS that works well with the methods generated (port, proxy, service..) from the WSDL file, but the other methods I need requires an authentication header like this:

<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <AuthHeader xmlns="http://localhost/webservices/map2">
      <UserName>string</UserName>
      <Password>string</Password>
      <Secure>boolean</Secure>
    </AuthHeader>
  </soap:Header>

after some research I found this solution by using SOAPHandler:

public boolean handleMessage(SOAPMessageContext context) {

        Boolean isRequest = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);

        if (isRequest) {

            try {
                SOAPMessage soapMsg = context.getMessage();
                SOAPEnvelope soapEnv = soapMsg.getSOAPPart().getEnvelope();
                SOAPHeader soapHeader = soapEnv.getHeader();

                if (soapHeader == null) {
                    soapHeader = soapEnv.addHeader();
                }

                // add a soap headers
                QName qnameAuthHeader = new QName("http://localhost/webservices/map2", "AuthHeader");

                SOAPHeaderElement soapAuthHeader = soapHeader.addHeaderElement(qnameAuthHeader);

                soapAuthHeader.setActor(SOAPConstants.URI_SOAP_ACTOR_NEXT);
                soapAuthHeader.addAttribute(new QName("http://localhost/webservices/map2", "UserName"), "user1");
                soapAuthHeader.addAttribute(new QName("http://localhost/webservices/map2", "Password"), "pass1");
                soapAuthHeader.addAttribute(new QName("http://localhost/webservices/map2", "Secure"), "false");


                soapMsg.saveChanges();

                // tracking
                soapMsg.writeTo(System.out);

            } catch (SOAPException e) {
                System.err.println(e);
            } catch (IOException e) {
                System.err.println(e);
            }

        }

        return true;
    }

but when I run this

ServiceSoapProxy proxy = new ServiceSoapProxy("http://localhost/ws3.0/service.asmx?wsdl");
        ServiceSoap service = proxy.getServiceSoap();

Binding binding = ((BindingProvider) service).getBinding();

I get this:

Exception in thread "main" java.lang.ClassCastException: localhost.webservices.map2.ServiceSoapStub cannot be cast to javax.xml.ws.BindingProvider
    at test.devs.MailiTest.main(MailiTest.java:33)

Solution

  • After reading my automatically generated stub, I finally understood and found the solution to my problem. and this answer is for others who are looking for the same thing that I was.

    I just added this line to createCall() method wich is located in my Stub.java:

    setHeader("http://localhost/webservices/map2", "AuthHeader", new AuthHeader("username", "password"));