带Cookie的Odata客户端 [英] Odata Client with Cookie
问题描述
已实施以下代码来从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("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;
}
}
}
同样,它不是完美的,但是应该演示如何获取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客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!