Search code examples
androidasp.netweb-servicessessionandroid-ksoap2

ksoap2 consuming .Net service with sessions


GetMessages is a method I have in a .net web service for my chat program. It gets a collection of messages, only sending new ones by saving the last message ID in a session variable.

HeaderProperty mSessionHeader = null;

public SoapObject GetMessages()
{
    ...
    SoapObject request = new SoapObject(NAMESPACE, METHOD);

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.V11);
    envelope.dotNet = true;
    envelope.bodyOut = request;

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

    // Build request header
    LinkedList<HeaderProperties> reqHeaders = new LinkList<HeaderProperties>();

    if (mSessionHeader != null) {
        reqHeaders.add(mSessionHeader); //using the session id from last header
    }

    List<HeaderProperties> respHeaders = transport.call(ACTION, envelope, reqHeaders);

    // See note after code
    for (HeaderProperty hp : respHeaders) {
        if (hp.getKey().equalsIgnoreCase("set-cookie"))
        {
            mSessionHeader = hp;
            break;
        }
    }

    return (SoapObject) envelope.getResponse();
}

the value of mSessionHeader is something like

set-cookie: ASP.NET_SessionId=0zzfwxxp1sakeafybjzsxp0k; path=/; HttpOnly

However, the GUID part changes every call to ... transport.call()
When I use the same header (by putting if mSessionHeader == null around the for loop) it still does not work... the method gets every message sent regardless of the position i set in the Web Service

Do I have to add more headers then just the ASP.NET_SessionId?


Solution

  • I'm surprised there isn't demand for an answer to this... anyways I found it.

    The request header to send a cookie is different from the response -

    Request

    "Cookie: ASP.NET_SessionId=#####"

    Response

    "Set-Cookie: ASP.NET_SessionId=#####"

    So the above line -

    reqHeaders.add(mSessionHeader);
    

    gets changed to

    reqHeaders.add(new HeaderProperty("Cookie", mSessionHeader.getValue());