繁体   English   中英

使用 RestSharp 保存 cookie

[英]Saving a cookie with RestSharp

我正在尝试使用 RestSharp 在 WinPhone 7 Mango 的 RestClients 和应用程序会话之间获取一个 cookie。

如果我使用单个 RestClient 实例,则 cookie 会持续存在。 我希望 cookie 在 RestClient 实例和用户返回应用程序之间持续存在。

RestSharp 最近添加了自动 cookie 支持!

RestSharp 102.4+ 支持对来自同一个 IRestClient 的所有请求使用共享的 System.Net.CookieContainer。 通过这样做,响应中设置或取消设置的任何 cookie 都将在后续请求中使用。 为了使用共享的 CookieContainer,只需在使用前设置 RestClient 实例的属性:

var client = new RestClient ("http://server/");
client.CookieContainer = new System.Net.CookieContainer();

文档:https://github.com/restsharp/RestSharp/wiki/Cookies

必须对 Cookie 值进行编码。

if ( header["Set-Cookie"] != null )
{
string value = header["Set-Cookie"];

Encoding iso = Encoding.GetEncoding("iso-8859-9");//may be utf-8

value = HttpUtility.UrlEncode(value, iso);
Cookie moCookie = new Cookie( "Cookie", value);
moCookie.Domain = GatewayURI.Trim();
CookieContainer CommCookie = new CookieContainer();
CommCookie.Add( moCookie );
request.CookieContainer = CommCookie;
}

您需要手动保存 cookie 信息,RestSharp 中不支持在会话之间保存 cookie。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM