0

I'm attempting to create an android app that makes a SOAP request. I've tried several different libraries (OkHTTP, kSoap2) to help me with this, and am currently trying to make classes generated through EasyWSDL work.

I'm able to create and send the request, but I can't seem to add the headers that I need. I'm adding them thusly, in the code:

HeaderProperty hProp1 = new HeaderProperty("Authorization", "Basic " +
            org.kobjects.base64.Base64.encode("########:#######".getBytes()));
HeaderProperty hProp2 = new HeaderProperty("Username", "########");
HeaderProperty hProp3 = new HeaderProperty("Password", "########");

service.httpHeaders.add(hProp2);
service.httpHeaders.add(hProp3);
service.httpHeaders.add(hProp1);

But in the end, the request that's generated lacks these headers:

<v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:d="http://www.w3.org/2001/XMLSchema" xmlns:c="http://www.w3.org/2003/05/soap-encoding" xmlns:v="http://www.w3.org/2003/05/soap-envelope">
<v:Header />
<v:Body>

Can anyone tell me what I'm doing wrong? Happy to provide further detail if needed.

Saurabh Bhandari
  • 2,438
  • 4
  • 26
  • 33

1 Answers1

0

I know, that this is old question but maybe I will help someone else. So httpHeaders collection is to add HTTP headers (not SOAP headers).

To add custom SOAP headers, the easiest way is to create a custom class which inherits from the generated service class. Then override createEnvelope method and set correct soap headers. In the code you should use this custom class to invoke a web service. Here is an example:

public class TestService extends BasicHttpBinding_IBodyArchitectAccessService
{
    @Override
    protected ExtendedSoapSerializationEnvelope createEnvelope() {
        ExtendedSoapSerializationEnvelope env= super.createEnvelope();
        env.headerOut = new org.kxml2.kdom.Element[2];
        org.kxml2.kdom.Element h = new org.kxml2.kdom.Element().createElement("","APIKey");
        h.addChild(org.kxml2.kdom.Node.TEXT, "123455677");

        org.kxml2.kdom.Element lang = new org.kxml2.kdom.Element().createElement("","Lang");
        lang.addChild(org.kxml2.kdom.Node.TEXT, "pl");

        env.headerOut[0]=h;
        env.headerOut[1]=lang;
        return env;
    }
}
robocik
  • 101
  • 7
  • 20