CookieContainer.GetCookies(Uri uri)上的ArgumentOutOfRangeException

时间:2011-12-30 10:54:00

标签: c# cookies cookiecontainer

在尝试获取给定网站的Cookie时,我遇到了这个奇怪的异常。 CookieContainer对象是单例类的成员,因此应用程序中的每个HttpWebRrequest都可以访问该站点的身份验证cookie。

    public string GetXsrfToken(string url)
    {
        Uri u = new Uri(url);
        CookieCollection cc;
        try
        {
            cc = this.Cookies.GetCookies(u);
        }
        catch
        {
            cc = this.Cookies.GetCookies(u);
        }

        string token =string.Empty;
        foreach (Cookie c in cc)
        {
            if (c.Name == "atlassian.xsrf.token")
            {
                token = c.Value;
                break;
            }
        }

        return token;
    }

http://pastebin.com/QaDSs2g5提供全班课程 第一次调用GetCookies会抛出ArgumentOutOfRangeException,其中包含以下堆栈跟踪:

at System.DateTime.Add(Double value, Int32 scale)
at System.TimeZoneInfo.TransitionTimeToDateTime(Int32 year, TransitionTime transitionTime)
at System.TimeZoneInfo.GetDaylightTime(Int32 year, AdjustmentRule rule)
at System.TimeZoneInfo.GetIsDaylightSavingsFromUtc(DateTime time, Int32 Year, TimeSpan utc, AdjustmentRule rule, Boolean& isAmbiguousLocalDst)
at System.TimeZoneInfo.GetDateTimeNowUtcOffsetFromUtc(DateTime time, Boolean& isAmbiguousLocalDst)
at System.DateTime.get_Now()
at System.Net.CookieCollection.TimeStamp(Stamp how)
at System.Net.CookieContainer.InternalGetCookies(Uri uri)
at System.Net.CookieContainer.GetCookies(Uri uri)
at JiraVersionSync.Core.CookieMgr.GetXsrfToken(String url) in C:\JIRA\dev\JiraVersionSync\JiraVersionSync.Core\CookieMgr.cs:line 46

DateTime.Add中导致此例外的参数为value,即null

但是第二个调用完美无缺,然后我就能找到我想要的cookie并且它的价值。所以我的代码有效,但我觉得它很难看,我很好奇它为什么第一次失败。任何想法,某人?

0 个答案:

没有答案