How to consume Session dependent WCF services using Ksoap2-Android

和自甴很熟 提交于 2019-12-04 22:22:08

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.

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