特定のサイトの 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
です。
しかし、2 番目の呼び出しは完全に機能し、必要な Cookie とその値を見つけることができます。したがって、私のコードは機能しますが、見苦しいと感じており、最初に失敗した理由について知りたいと思っています。アイデアはありますか?