Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to consume Session dependent WCF services using Ksoap2-Android

I am using Ksoap2-Android for consuming the WCF Services.

For the dotnet client we keep the allowCookies="true" in our binding configuration and it sends the same sessionid and keeps my sessions intact in my WCF services (My services are interdependent and use the sessions).

Any one know any such setting for ksoap2-android, that will allow me to consume the WCF service keeping my session intact on the server.

Currently when i make a new call to the service, the sessionid gets changed and all my session variables clear out and loose their values.

like image 709
Vipul Avatar asked Jun 23 '10 11:06

Vipul


1 Answers

In C# i do the next, just use the android methods to do this:

1.- Make the Http request, 2.- Make a Cookie Container of the first request. 3.- Put the cookieContainer over the second request, for example you can put in a bundle in a intent for the 2nd activity, and use this cookies for send the second http request...

My C# Code;

protected static void GetData()
    {
        CookieContainer cookies = new CookieContainer();
        HttpWebRequest request1 = (HttpWebRequest)WebRequest.Create("https://any.com/url");
        request1.CookieContainer = cookies;
        HttpWebResponse response1 = (HttpWebResponse)request1.GetResponse();
        StreamReader responseReader1 = new StreamReader(response1.GetResponseStream());
        Response1 = responseReader1.ReadToEnd();
        responseReader1.Close();
        responseReader1.Dispose();

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
        request.CookieContainer = cookies;
        request.Method = "GET";
        request1.KeepAlive = true;
        try
        {
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader responseReader = new StreamReader(response.GetResponseStream());
            Response = responseReader.ReadToEnd();
            responseReader.Close();
            responseReader.Dispose();
            if (Response.Contains("Server Error in '/Verification' Application."))
            {
                Console.WriteLine("Empty Registry" + Url);
            }
        }
        catch (WebException ex)
        {
            if (ex.Response != null)
            {
                Console.WriteLine("Failed at: " + Url);
            }
            if (ex.Status == WebExceptionStatus.ProtocolError)
            {
                if (((HttpWebResponse)ex.Response).StatusCode == HttpStatusCode.NotFound)
                {
                    Console.WriteLine(ex.Status);
                }
            }
            else if (ex.Status == WebExceptionStatus.NameResolutionFailure)
            {
                Console.WriteLine(ex.Status);
            }

        }
    }

I do That for keep the sesionID of the first request, and later, in the second request, i add the cookieContainer (because the server requires me) (to make a bot search) ;)... hope this give you ideas.

like image 50
JLouis Avatar answered Nov 12 '22 00:11

JLouis