Trying to build a correct SOAP Request

前端 未结 4 598
滥情空心
滥情空心 2020-12-10 04:16

I have been struggling for hours trying to build the correct SOAP request using ksoap2 for Android with no luck. The ideal request looks like this:



        
4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-10 04:21

    I think you need another way to create the header, it looks like jax-ws, so i'll go with a jax ws implementation i did a couple of months ago.

    First you need a HeaderHandler class , wich creates the soap header element, it should look like this:


        import javax.xml.namespace.QName;
        import javax.xml.soap.SOAPElement;
        import javax.xml.soap.SOAPEnvelope;
        import javax.xml.soap.SOAPHeader;
        import javax.xml.ws.handler.MessageContext;
        import javax.xml.ws.handler.soap.SOAPHandler;
        import javax.xml.ws.handler.soap.SOAPMessageContext;
    
    
        public class HeaderHandler implements SOAPHandler {
    
            public boolean handleMessage(SOAPMessageContext smc) {
                Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
                String AUTH_TK = "http://www.avectra.com/2005/";
                String PREFIX="";//no prefix
                String PREFIX_XMLNS="xmlns";
                String value =  "123456";
                if (outboundProperty.booleanValue()) {
                    try {
                        SOAPEnvelope envelope = smc.getMessage().getSOAPPart().getEnvelope();
                        SOAPHeader header = envelope.addHeader();
                        //
                        SOAPElement authorizationToken = header.addChildElement("AuthorizationToken", PREFIX_XMLNS, AUTH_TK);
                        //value
                        SOAPElement usernameToken =
                            authorizationToken.addChildElement("Token", PREFIX);
                            usernameToken.addTextNode(value);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                return outboundProperty;
            }
    
    
            public Set getHeaders() {
                return null;
            }
    
            public void close(MessageContext arg0) {
    
            }
    
            public boolean handleFault(SOAPMessageContext arg0) {
                return false;
            }
        }
    

    After that you create a HeaderHandlerResolver to handle the header creation and insert it in a handler chain:


        import java.util.ArrayList;
        import java.util.List;
        import javax.xml.ws.handler.Handler;
        import javax.xml.ws.handler.HandlerResolver;
        import javax.xml.ws.handler.PortInfo;
    
        public class HeaderHandlerResolver implements HandlerResolver {
    
        @SuppressWarnings("unchecked")
        public List getHandlerChain(PortInfo portInfo) {
              List handlerChain = new ArrayList();
              HeaderHandler hh = new HeaderHandler();
              handlerChain.add(hh);
              return handlerChain;
           }
        }
    

    After that, you add in the Client:


            try{
                //new service instance (your service should be extending javax.xml.ws.Service;)
                YourServiceProxy service = new YourServiceProxy();
                //calls the header handler resolver ;)
                service.setHandlerResolver(new HeaderHandlerResolver());
                //get the service
                YourService port = (YourService)service.getYourService();
                //call the service 
                port.yourMethod()   
            } catch (Exception e) {
                e.printStackTrace();
            }
    

    By the way, i didn't tested this particular header, i modified a previous header handler i had, so it may be not accurate, but i think it's pretty close, i really hope it helps you, try it out and tell us how it comes, i'll try to help you if it still doesn't works.

提交回复
热议问题