开发者

KSoap2 Android not valid SOAP

开发者 https://www.devze.com 2022-12-25 17:03 出处:网络
I am trying to post to my own test soap server (C#) with Android in combination with KSOAP2. Now I have the specifications from the SOAP server, it expects:

I am trying to post to my own test soap server (C#) with Android in combination with KSOAP2.

Now I have the specifications from the SOAP server, it expects:

POST /SharingpointCheckBarcode.asmx HTTP/1.1
Host: awc.test.trin-it.nl
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/checkBarcode"

<?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>
    <AuthHeader xmlns="http://tempuri.org/">
      <username>string</username>
      <password>string</password>
    </AuthHeader>
  </soap:Header>
  <soap:Body>
    <checkBarcode xmlns="http://tempuri.org/">
      <barcode>string</barcode>
    </checkBarcode>
  </soap:Body>
</soap:Envelope>  

But what Android KSOAP2 sends out:

<?xml version="1.0" encoding="utf-8"?>
    <v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:d="http://www.w3.org/2001/XMLSchema" xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" xmlns:v="http://schemas.xmlsoap.org/soap/envelope/">
        <v:Header />
        <v:Body>
            <checkBarcode xmlns="http://tempuri.org" id="o0" c:root="1">
                <username i:type="d:string">test</username>
                <password i:type="d:string">test</password>
                <barcode i:type="d:string">2620813000301</barcode>
            </checkBarcode>
开发者_高级运维        </v:Body>
    </v:Envelope>

With this code:

    try {
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

        request.addProperty("username", "test");
        request.addProperty("password", "test");
        request.addProperty("barcode", "2620813000301");

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.encodingStyle = "test";

        envelope.setOutputSoapObject(request);

        AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport (URL); 
        androidHttpTransport.debug = true;
        androidHttpTransport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"utf-8\"?>");

        androidHttpTransport.call(SOAP_ACTION, envelope);
        Log.d("MyAPP", "----------------- " + androidHttpTransport.requestDump +"\r\n\r\n" + androidHttpTransport.responseDump);
        ((TextView)findViewById(R.id.lblStatus)).setText(androidHttpTransport.requestDump +"\r\n\r\n" + androidHttpTransport.responseDump);
    } catch(Exception E) {
        ((TextView)findViewById(R.id.lblStatus)).setText("ERROR:" + E.getClass().getName() + ": " + E.getMessage());
    }

The response I get back from the server is that there are no results found, so not an error, but when I test it with another App or PHP, it with the same data, it says it's OK.

I think it's because of the


When you use addProperty this automatically adds it to the soap body, so thats wrong in your sample.

If you want to set up a username/password security header you have to build up the necessary Element[] and set it with as headerOut on your envelope.

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.headerOut = security;

To build up security as an Element[] you use something along these lines

        Element usernameElement = new Element().createElement(OASIS_SECURITY_XSD_URL, "Username");
        usernameElement.addChild(Node.TEXT, username);
        Element passwordElement = new Element().createElement(OASIS_SECURITY_XSD_URL, "Password");
        passwordElement.addChild(Node.TEXT, password);

        Element usernameTokenElement = new Element().createElement(OASIS_SECURITY_XSD_URL, "UsernameToken");
        usernameTokenElement.addChild(Node.ELEMENT, usernameElement);
        usernameTokenElement.addChild(Node.ELEMENT, passwordElement);

        Element securityElement = new Element().createElement(OASIS_SECURITY_XSD_URL, "Security");
        securityElement.setPrefix(null, OASIS_SECURITY_XSD_URL);
        securityElement.addChild(Node.ELEMENT, usernameTokenElement);

and add it all up in an Element[] before you set it to headerOut

0

精彩评论

暂无评论...
验证码 换一张
取 消