问题
First, I apologize for asking a question that is already common here in the SOF.
But I am a beginner and I'm certainly cruel.
I am creating an android application that communicates with a WS. So I can make requests to the WS, I have to add a value to the header of the envelope, but I can not add.
I found some answers about it here in the SOF, however, could not fully understand how it works. Perhaps, my doubts are due to the nodes of the header, which ended up confusing me even more.
One of the answers I found I ended up not helping: "How to set soap header using ksoap2 android"
Below is the XML request that needs to be done:
?xml version="1.0" encoding="utf-8"?
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
ValidationSoapHeader xmlns="http://tempuri.org/"
DevToken>string/DevToken
/ValidationSoapHeader
/soap:Header
soap:Body
ListaCidades xmlns="http://tempuri.org/" /
/soap:Body
/soap:Envelope
And my code below:
SoapObject request = new SoapObject(ApplicationData.NAMESPACE, ApplicationData.METHOD_NAME_LISTA_CIDADES);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
How exactly do I use the envelope.HeaderOut? Is it really necessary to create a helper method to build an Element even having to pass only one parameter (DevToken)?
Thank you for your attention!
回答1:
Element h = new Element().createElement(NAMESPACE, "AuthHeader");
Element Username = new Element().createElement(NAMESPACE, "Username");
Username.addChild(Node.TEXT, "CBROWN");
h.addChild(Node.ELEMENT, Username);
Element wssePassword = new Element().createElement(NAMESPACE, "wssePassword");
wssePassword.addChild(Node.TEXT, "welcome");
h.addChild(Node.ELEMENT, wssePassword);
envelope.headerOut = new Element[]{h};
add above code for add header in envelope
来源:https://stackoverflow.com/questions/11571645/set-soap-header-ksoap2-android