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?