System.Web.HttpContext.Current throws IndexOutOfRangeException error
<p>I am getting an error that doesn't happen very often, but the client has seen it and wants it fixed.</p>

<p>Basically, it's an <code>IndexOutOfRangeException</code> exception, but what's odd is that the trace points to <code>System.Web.HttpContext.Current</code>. How is that possible?</p>

<p>The line that it fails on is like this:</p>

<pre><code>System.Web.HttpContext context = System.Web.HttpContext.Current;
</code></pre>

<p>If it was an array, I could do counts and other checks, but what kind of check would I do here other than a try-catch?</p>

<h2>UPDATE</h2>

<p>Here is the error that I am getting:</p>

<pre><code>Hostname:              www.mysite.com
    User:                  (none)
    Email:                 ..
    Top-level URL:         /services/flash/prize.asmx
    Error occurred in:     top level or layout

    Error description:     System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at mysite.MainSite.Web.EnHomeController.HomepageActivity.GetCookieHash() in d:\www\staging.mysite.com\Web\en\home\Controller.cs:line 511
   at mysite.MainSite.Web.EnHomeController.HomepageActivity.SetCompletedCookieValue(Boolean is_completed) in d:\www\staging.mysite.com\Web\en\home\Controller.cs:line 547
   at mysite.MainSite.Web.Services.prize.Submit(String gameID, String homepageID, Int32 pointsUsed) in d:\www\staging.mysite.com\Web\services\flash\prize.cs:line 122


    Server Name:           MyServer
    Date:                  Monday, May 02, 2011 8:13:23 PM
    Https? :               off
    Url:                   http://www.mysite.com/services/flash/prize.asmx
    Form:                  
    Query String:          
    Referrer:              http://www.mysite.com/local/files/en/home/wof/container_v25.swf
    Remote Address:        xxx.xxx.xxx.xxx
    Remote Host:           xxx.xxx.xxx.xxx
    Request Method:        POST
    Local Time:            5/2/2011 8:13:23 PM
</code></pre>

<p>The reason why I didn't release it is because it doesn't tell much. All those lines are essentially function calls. Hopefully this will help out a bit more. The worst part about this is that I can't even reproduce it, so I'm just playing the guessing game.</p>

<h2>Update</h2>

<p>Here is the function. Hopefully it will help, but It might not be too helpful:</p>

<pre><code>private ObjectHash GetCookieHash() {
                System.Web.HttpContext context = System.Web.HttpContext.Current;

                HttpCookie cookie = context.Response.Cookies[COOKIE_NAME];
                if (!HaveCookie(cookie)) {
                    cookie = context.Request.Cookies[COOKIE_NAME];
                    context.Response.Cookies.Set(cookie);
                }

                if (!HaveCookie(cookie)) {
                    return new ObjectHash();
                }
                else {
                    string v = HttpUtility.UrlDecode(cookie.Value);
                    ObjectHash h = new ObjectHash();
                    foreach (string key_val in v.Split('&amp;')) {
                        string[] t = key_val.Split('=');
                        h[t[0]] = t[1];
                    }

                    return h;
                }
            }
</code></pre>

<p>It's failing on the first line of the function.</p>
