how to set soap Header using ksoap2 - android

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 07:37:30

问题


I need to set the soap header information as part of authentication of a web method. I\'m using ksoap2 API to call .NET web service. Here is the soap header with request.

<soap:Header>
    <DTHeader xmlns=\"http://myServer.com/webservices/\">
      <Username> string </Username>
      <Password> string </Password>
    </DTHeader>
</soap:Header>
<soap:Body>
    <MyTestMethod xmlns=\"http://myServer.com/webservices/\">
       <ID> string </ID>
       <TransID> guid </TransID>
     </MyTestMethod>
</soap:Body>

Can you please provide the android code to set the soap header \"DTHeader\" and set \"Username\" and \"Password\".


回答1:


I did that this way:

import org.kxml2.kdom.Element;

then while preparing envelope

soapEnvelope.headerOut = new Element[1];
soapEnvelope.headerOut[0] = buildAuthHeader();
// ...send request...

with

private Element buildAuthHeader() {
    Element h = new Element().createElement(NAMESPACE, "AuthHeader");
    Element username = new Element().createElement(NAMESPACE, "user");
    username.addChild(Node.TEXT, USERNAME);
    h.addChild(Node.ELEMENT, username);
    Element pass = new Element().createElement(NAMESPACE, "pass");
    pass.addChild(Node.TEXT, PASSWORD);
    h.addChild(Node.ELEMENT, pass);

    return h;
}

obviously, change strings as needed.




回答2:


Spent 2 days trying to get this to work with java and .net and have finally got it working..

ArrayList<HeaderProperty> headerProperty = new ArrayList<HeaderProperty>();

headerProperty.add(new HeaderProperty("guid", "value..."));

androidHttpTransport.call(soap_action,envelope,headerProperty); 

tried the above example and would add a header section but wasn't compatible with .net.

My piece of code works but requires KSoap2 version ksoap2-android-assembly-2.5.7-jar-with-dependencies.jar from here: ksoap jar file location

right click and view raw file and click save as.

Thanks for all the help in the forums as pointed me in the right direction...



来源:https://stackoverflow.com/questions/5613675/how-to-set-soap-header-using-ksoap2-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!