带Cookie的Odata客户端

编程入门 行业动态 更新时间:2024-10-12 03:24:06
本文介绍了带Cookie的Odata客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

已实施以下代码来从Cookies中获取JsessioniD.该网站使用表单身份验证.

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

这就是我所实施的.

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; } }

我得到了JSessionID,现在的问题是如何用cookie头调用odata客户端?

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

推荐答案

我没有在此PC上安装Xamarin进行测试,但应该类似于以下内容.

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

并没有太大的变化,只是检查每个响应上的.ASPXAUTH cookie,如果存在,则将其存储.对于每个请求,如果有一个存储的cookie值,它将被添加到请求中.

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.

更新

以前没有使用过Simple.OData,但查看API我相信您应该能够执行以下操作:

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("www.your-odata-url"); 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; } } }

同样,它不是完美的,但是应该演示如何获取auth cookie,然后将其应用于OData请求.

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

更多推荐

带Cookie的Odata客户端

本文发布于:2023-11-28 12:00:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1642321.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:客户端   Cookie   Odata

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!