Odata Client with Cookie

青春壹個敷衍的年華 提交于 2019-12-12 03:07:38

问题


The following code has been implemented to get JsessioniD from the Cookies. The webSite uses form authentication.

Here is what I have implemented.

public override void ViewDidLoad ()
  {
    base.ViewDidLoad ();
    using(var client= new CookieAwareWebClient())
    {
        var values= new NameValueCollection
           {
             {"username","admin"},
             {"password","admin"},
           };
            client.UploadValues("myURL/j_security_check",values);
            Cookie jSessionID = client.ResponseCookies["JSESSIONID"];
            if (jSessionID != null)
            {
                // get the JEssionID here
               string value = jSessionID.Value;
             }
    };
}

public class CookieAwareWebClient : WebClient
{
    public CookieAwareWebClient()
    {
        CookieContainer = new CookieContainer();
        this.ResponseCookies = new CookieCollection();
    }

    public CookieContainer CookieContainer { get; private set; }
    public CookieCollection ResponseCookies { get; set; }

    protected override WebRequest GetWebRequest(Uri address)
    {
        var request = (HttpWebRequest)base.GetWebRequest(address);
        request.CookieContainer = CookieContainer;
        return request;
    }

    protected override WebResponse GetWebResponse(WebRequest request)
    {
        var response = (HttpWebResponse)base.GetWebResponse(request);
        this.ResponseCookies = response.Cookies;
        return response;
    }
}

I get the JSessionID, and now my question is how to make call to odata client with cookie header?


回答1:


I don't have Xamarin installed on this PC to test but should be something like below.

Haven't changed a great deal, just checking for an .ASPXAUTH cookie on each response and if it exists, storing it. For each request, if there's a stored cookie value, this gets added to the request.

public override void ViewDidLoad()
{
    base.ViewDidLoad();

    using (var client = new CookieAwareWebClient())
    {
        var values = new NameValueCollection
        {
            {"username", "admin"},
            {"password", "admin"},
        };

        // Make call to authenticate
        client.UploadValues("myURL/j_security_check", values);

        // Make call to get data or do something as authenticated user
        var dataYouWant = client.DownloadString("myURL/page_with_data");
    }
}

public class CookieAwareWebClient : WebClient
{
    private const string COOKIEKEY = ".ASPXAUTH";
    public string AspAuthCookieValue { get; set; }

    protected override WebRequest GetWebRequest(Uri address)
    {
        var request = (HttpWebRequest) base.GetWebRequest(address);

        if (!string.IsNullOrEmpty(AspAuthCookieValue))
        {
            request.CookieContainer.Add(new Cookie
            (
                COOKIEKEY,
                AspAuthCookieValue
            ));
        }

        return request;
    }

    protected override WebResponse GetWebResponse(WebRequest request)
    {
        var response = (HttpWebResponse) base.GetWebResponse(request);
        var authCookie = response.Cookies[COOKIEKEY];

        if (authCookie != null && !string.IsNullOrEmpty(authCookie.Value))
        {
            AspAuthCookieValue = authCookie.Value;
        }

        return response;
    }
}

Isn't perfect but should give you an idea of what's required.

UPDATE

Haven't used Simple.OData before but looking at APIs I believe you should be able to do something similar to:

public class GetSomeOData
{
    private const string COOKIEKEY = ".ASPXAUTH";
    private string _cookieValue;

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        using (var client = new CookieAwareWebClient())
        {
            var values = new NameValueCollection
            {
                {"username", "admin"},
                {"password", "admin"},
            };

            // Make call to authenticate
            client.UploadValues("myURL/j_security_check", values);

            _cookieValue = client.AspAuthCookieValue;

            // Make call to get data or do something as authenticated user
            var dataYouWant = GetOData();
        }
    }

    public dynamic GetOData()
    {
        var feed = new ODataFeed("http://www.your-odata-url.com");
        feed.BeforeRequest += AddCookieToRequest;
        var db = Database.Opener.Open(feed);

        return db.SomeData.FindById(1234);
    }

    public void AddCookieToRequest(HttpRequestMessage request)
    {
        request.Headers.Add("Cookie", String.Format("{0}={1}", COOKIEKEY, _cookieValue));
    }

    public class CookieAwareWebClient : WebClient
    {
        public string AspAuthCookieValue { get; set; }

        protected override WebResponse GetWebResponse(WebRequest request)
        {
            var response = (HttpWebResponse)base.GetWebResponse(request);
            var authCookie = response.Cookies[COOKIEKEY];

            if (authCookie != null && !String.IsNullOrEmpty(authCookie.Value))
            {
                AspAuthCookieValue = authCookie.Value;
            }

            return response;
        }
    }
}

Again, it's not perfect but should demonstrate how you get the auth cookie and then apply this to your OData requests.



来源:https://stackoverflow.com/questions/29494246/odata-client-with-cookie

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