android ksoap2信封

时间:2012-01-04 00:59:35

标签: android ksoap2

我正在尝试使用kso​​ap2来使用.net网络服务。

这是成功请求的信封:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
     <s:Header>
      <o:Security s:mustUnderstand="1" xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
            <u:Timestamp u:Id="_0">
                <u:Created>2011-12-28T18:36:08.662Z</u:Created>
                <u:Expires>2011-12-28T18:41:08.662Z</u:Expires>
            </u:Timestamp>
            <o:UsernameToken u:Id="the-uuid">
                <o:Username>username</o:Username>
                <o:Password o:Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">password</o:Password>
            </o:UsernameToken>
        </o:Security>
    </s:Header>
    <s:Body>
        <WebServiceMethod xmlns="http://webservicenamespace"/>
    </s:Body>
</s:Envelope>

这是我的信封由ksoap2生成

<v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:d="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" xmlns:v="http://schemas.xmlsoap.org/soap/envelope/">
    <v:Header>
        <n0:Security v:mustUnderstand="1" xmlns:n0="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
            <d:Timestamp d:Id="_0">
                <d:Created>2012-01-04T00:39:10.000Z</d:Created>
                <d:Expires>2012-01-04T00:44:10.000Z</d:Expires>
            </d:Timestamp>
            <n0:UsernameToken d:Id="the-uuid">
                <n0:Username>username</n0:Username>
                <n0:Password>password</n0:Password>
            </n0:UsernameToken>
        </n0:Security>
    </v:Header>
    <v:Body>
        <WebServiceMethod xmlns="webservicenamespace" />
    </v:Body>
</v:Envelope>

这是我用来生成上述请求的代码:

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelope.dotNet = true;
soapEnvelope.setAddAdornments(false);
soapEnvelope.implicitTypes = true;
soapEnvelope.setOutputSoapObject(request);

HttpTransportSE transport = new HttpTransportSE(URL);
transport.debug = true;

soapEnvelope.xsd = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd";
String oNS = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";
             Element[] header = new Element[1]; 
header[0] = new Element().createElement(oNS, "Security");
header[0].setAttribute(soapEnvelope.env, "mustUnderstand", "1");

Element timestamp = new Element().createElement(soapEnvelope.xsd, "Timestamp");
timestamp.setAttribute(soapEnvelope.xsd, "Id", "_0");


String now[] = getTimestamp();
Element created = new Element().createElement(soapEnvelope.xsd, "Created");
created.addChild(Node.TEXT, now[0]);
Element expires = new Element().createElement(soapEnvelope.xsd, "Expires");
expires.addChild(Node.TEXT, now[1]);

timestamp.addChild(Node.ELEMENT, created);
timestamp.addChild(Node.ELEMENT, expires);

Element userNameToken = new Element().createElement(oNS, "UsernameToken");
userNameToken.setAttribute(soapEnvelope.xsd, "Id", "the-uuid");

Element username = new Element().createElement(oNS, "Username");
username.addChild(Node.TEXT, "username");
Element password = new Element().createElement(oNS, "Password");
password.addChild(Node.TEXT, "passwor");

userNameToken.addChild(Node.ELEMENT, username);
userNameToken.addChild(Node.ELEMENT, password);

header[0].addChild(Node.ELEMENT, timestamp);
header[0].addChild(Node.ELEMENT, userNameToken);

soapEnvelope.headerOut = header;

我从服务器收到400 - 错误请求...我做错了什么?

1 个答案:

答案 0 :(得分:0)

400响应似乎是一个问题之前 SOAP,即Http请求,see here for error codes。检查连接代码(HTTPTransport)。

希望它有所帮助。