Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

KSoap2 Android not valid SOAP

Tags:

android

soap

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

like image 847
Roger Far Avatar asked Apr 02 '10 09:04

Roger Far


1 Answers

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

like image 93
Manfred Moser Avatar answered Sep 22 '22 22:09

Manfred Moser