ArgumentOutOfRangeException on CookieContainer.GetCookies(Uri uri)
Asked Answered
R

0

1

I have this strange exception when trying to get the cookies for a given site. The CookieContainer object is a member of a singleton class, so that each HttpWebRrequest across the application has the access to the authentication cookies for the site.

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

Full class is available at http://pastebin.com/QaDSs2g5.
The first call to GetCookies throws a ArgumentOutOfRangeException with the following stack trace:

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

The parameter causing this exception in DateTime.Add is value, which is null.

But the second call works perfectly and I'm then able to find the cookie I want and it's value. So my code works, but I feel it's ugly and I'm curious as to why it fails the first time. Any idea, someone?

Rochet answered 30/12, 2011 at 10:53 Comment(5)
That try/catch looks realty weird.Goodbye
It seems like DateTime.Now has crashed in an attempt to save the world and prevent 2012 from ever happening...Martinet
There isn't enough here, what are the values of value and scale?Adeno
Edited, this is caused by value being null. I don't know if I can have the value of scale.Rochet
How can value, a Double, be null? That'd just be 0.0, which wouldn't be a problem here. (I am adding this comment without Reflector being available to me at the moment, so I might delete this later...)Ferula

© 2022 - 2024 — McMap. All rights reserved.