How to consume Session dependent WCF services using Ksoap2-Android

[亡魂溺海] 提交于 2019-12-10 00:40:24

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/3101059/how-to-consume-session-dependent-wcf-services-using-ksoap2-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!