重用 CookieContainer 时如何确定会话超时
我有以下代码,它重复使用 CookieContainer,该 CookieContainer 在第一个请求时登录,但仅使用 cookie 容器来处理之后的请求。
一段时间后,如果空闲,站点将给出会话超时,我将需要再次执行登录。
问:我可以(使用 cookie 容器对象)确定是否发生超时,或者最好从恰好包含“会话超时”等文本的 HttpWebResponse 确定是否发生超时。 做这个的最好方式是什么?
private static CookieContainer _cookieContainer;
private static CookieContainer CurrentCookieContainer
{
get
{
if (_cookieContainer == null || _cookieContainer.Count == 0)
{
lock (_lock)
{
if (_cookieContainer == null || _cookieContainer.Count == 0)
{
//_cookieContainer.GetCookies(
_cookieContainer = DoLogin();
}
}
}
return _cookieContainer;
}
set
{
_cookieContainer = value;
}
}
然后这个方法调用容器:
public static string SomeMethod(SomeParams p)
{
HttpWebRequest request_thirdPartyEnquiryDetails = (HttpWebRequest)WebRequest.Create(thirdPartyEnquiryDetails);
CookieContainer cookieContainer = CurrentCookieContainer;
request_thirdPartyEnquiryDetails.CookieContainer = cookieContainer;
//... and it goes on to submit a search and return the response
}
I have the following code which re-uses a CookieContainer which logs in on the first request, but just uses the cookie container for requests after.
After a period of time if idle the site will give a Session Timeout, I will need to perform the login again.
Q: Can I determine (with the cookie container object) if the timeout has happened or is it best to determine if it has happened from the HttpWebResponse which happens to contains text like 'session timeout'. What is the best way to do this?
private static CookieContainer _cookieContainer;
private static CookieContainer CurrentCookieContainer
{
get
{
if (_cookieContainer == null || _cookieContainer.Count == 0)
{
lock (_lock)
{
if (_cookieContainer == null || _cookieContainer.Count == 0)
{
//_cookieContainer.GetCookies(
_cookieContainer = DoLogin();
}
}
}
return _cookieContainer;
}
set
{
_cookieContainer = value;
}
}
And then this method calls out to the container:
public static string SomeMethod(SomeParams p)
{
HttpWebRequest request_thirdPartyEnquiryDetails = (HttpWebRequest)WebRequest.Create(thirdPartyEnquiryDetails);
CookieContainer cookieContainer = CurrentCookieContainer;
request_thirdPartyEnquiryDetails.CookieContainer = cookieContainer;
//... and it goes on to submit a search and return the response
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,由于超时时间是 30 分钟,所以我将登录设置为 25 分钟后重复登录。
作为额外的预防措施,我检查 HttpResponse 中是否有页面会话超时时返回的文本(尽管现在预计这不会被看到)。 如果发生这种情况,我将 lastLoggedIn 日期设置为 null 并再次运行搜索方法。
Well, since the timeout is 30 mins, I have set the login to repeat after 25 mins.
As an extra precaution, I check the HttpResponse for the text which returns when the page session times out (although its now expected this won't be seen). If this happens I set the lastLoggedIn date to null and run the search method again.
您可以使用 CookieContainer.GetCookies(string uri) 方法提取域的所有 cookie。 使用此 CookieCollection,您可以获取您感兴趣的 cookie,并检查其 Expired 属性以查看它是否已过期。
您应该注意一件事:即使您的 cookie 有效,您的会话也可能会结束。 IIS 可能会重新启动 Web 应用程序在其中运行的应用程序域,在这种情况下,所有经过身份验证的用户都可能会丢失其会话数据。 因此,检查 cookie 通常不足以确保您保持登录状态。
You can extract all cookies for a domain using the CookieContainer.GetCookies(string uri) method. Using this CookieCollection you can get the cookie you are interested in and check its Expired property to see if it has expired.
There is one thing you should note: Your session may end even if your cookie is valid. IIS may restart the app domain the web application runs in and in that case all authenticated users may loose their session data. So checking the cookie is generally not enough to ensure that you stay logged in.
我不确定你想要实现什么,但你应该注意到 CookieContainer 在 .Add(Cookie) 和 .GetCookies(uri) 方法上有一个错误。
请在此处查看详细信息并修复:
http ://dot-net-expertise.blogspot.com/2009/10/cookiecontainer-domain-handling-bug-fix.html
I am not sure what you want to achieve, but you should notice that CookieContainer has a bug on .Add(Cookie) and .GetCookies(uri) method.
See the details and fix here:
http://dot-net-expertise.blogspot.com/2009/10/cookiecontainer-domain-handling-bug-fix.html