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?

Antoine
  • 5,055
  • 11
  • 54
  • 82
  • 3
    That try/catch looks realty weird. – H H Dec 30 '11 at 11:01
  • It seems like `DateTime.Now` has crashed in an attempt to save the world and prevent 2012 from ever happening... – C.Evenhuis Dec 30 '11 at 11:02
  • There isn't enough here, what are the values of `value` and `scale`? – m.edmondson Dec 30 '11 at 11:40
  • Edited, this is caused by `value` being `null`. I don't know if I can have the value of `scale`. – Antoine Dec 30 '11 at 11:52
  • 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...) – Mark Hurd May 26 '12 at 11:45

0 Answers0