private void PostRequest(string requestURL, byte[] requestPostData, Character c)
        {
            //const int bufferSize = 2048;

            //byte[] responseBuffer = new byte[bufferSize];
            try
            {
                HttpWebRequest request = MakeDKRquest(requestURL, requestPostData);
                ParseResponse(request, c);
                //using (WebResponse response = request.GetResponse())
                //using (Stream responseStream = response.GetResponseStream())
                //{
                //    responseStream.Read(responseBuffer, 0, bufferSize);
                //}
            }
            catch (Exception ex)
            {
                ManageConnectionException(ex);
            }
        }
        private void UpdateTextAsyc(Character c = null)
        {
            if (this.InvokeRequired)
            {
                DelDefUpdateText del = UpdateTextAsyc;
                this.BeginInvoke(del, c);
            }
            else
            {
                StringBuilder sb = new StringBuilder();

                if (c != null)
                {
                    if (SleepingForTurns)
                    {
                        lblAction.Text =
                            "Sleeping Until: " +
                            DateTime.Now.AddMinutes((double)(MaxAcceptableTurns - c.CurrentTurns) / (double)c.TurnsPerMin)
                            .ToShortTimeString();
                    }
                    else if (farmMonstersWorkerThread != null && farmMonstersWorkerThread.IsAlive)
                    {
                        lblAction.Text = c.GetNextMonsterFarmAction().Action.ToString();
                    }
                    else if (farmResourcesWorkerThread != null && farmResourcesWorkerThread.IsAlive)
                    {
                        lblAction.Text = c.GetNextRangerFarmAction().Action.ToString();
                    }
                    else
                    {
                        lblAction.Text = "UNKNOWN";
                    }

                    lblTurns.Text = c.CurrentTurns.ToString();
                    lblGold.Text = c.CurrentGold.ToString("#,0,") + "k";
                    lblBankGold.Text = c.CurrentGoldBank.ToString("#,0,") + "k";
                    lblDragonPoints.Text = c.CurrentDragonPoints.ToString("#,0");
                    lblBankDragonPoints.Text = c.CurrentDPBank.ToString("#,0");
                    lblCycles.Text = PeriodsCompleted.ToString("#,#");

                    lblFish.Text = c.CurrentFishCount.ToString("#,0,") + "k";
                    lblIron.Text = c.CurrentIronCount.ToString("#,0,") + "k";
                    lblStone.Text = c.CurrentStoneCount.ToString("#,0,") + "k";
                    lblWood.Text = c.CurrentWoodCount.ToString("#,0,") + "k";

                    if (c.CurrentLocation.Y < 0)
                    {
                        sb.Append(c.CurrentLocation.Y * -1);
                        sb.Append("S,");
                    }
                    else
                    {
                        sb.Append(c.CurrentLocation.Y);
                        sb.Append("N,");
                    }
                    if (c.CurrentLocation.X < 0)
                    {
                        sb.Append(c.CurrentLocation.X * -1);
                        sb.Append('W');
                    }
                    else
                    {
                        sb.Append(c.CurrentLocation.X);
                        sb.Append('E');
                    }
                    lblLocation.Text = sb.ToString();
                    sb.Clear();
                }

                sb.Append("Total Execption Count: ");
                sb.AppendLine(exceptionCount.ToString());
                sb.AppendLine();
                sb.AppendLine("Exception Queue:");

                Tuple<Exception, DateTime>[] exceptionList = new Tuple<Exception, DateTime>[lastExceptions.Count];
                lastExceptions.CopyTo(exceptionList, 0);

                for (int i = exceptionList.Length - 1; i >= 0; i--)
                {
                    sb.AppendLine(exceptionList[i].Item2.ToString());
                    sb.AppendLine(exceptionList[i].Item1.ToString());
                    sb.AppendLine();
                }

                tbOut.Text = sb.ToString();
            }
        }
        private Character ParseResponse(HttpWebRequest request, Character c = null)
        {
            string fullResponse = null;

            if (c == null)
                c = new Character();

            byte[] responseBuffer = new byte[2048];
            int byteCount = 0;
            StringBuilder responseString = new StringBuilder();
            Decoder decoder = Encoding.UTF8.GetDecoder();

            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            using (Stream responseStream = response.GetResponseStream())
            {
                do
                {
                    byteCount = responseStream.Read(responseBuffer, 0, responseBuffer.Length);

                    char[] chars = new char[decoder.GetCharCount(responseBuffer, 0, byteCount)];
                    decoder.GetChars(responseBuffer, 0, byteCount, chars, 0);
                    responseString.Append(chars);
                }
                while (byteCount > 0);

                fullResponse = responseString.ToString();

                if (response.ResponseUri.ToString().Contains("fight"))
                {
                    c.InCombat = true;
                    if (response.ResponseUri.ToString().Contains("boss"))
                    {
                        c.FightingBoss = true;
                    }
                    else
                    {
                        c.FightingBoss = false;
                    }
                }
                else
                {
                    if (c.InCombat)
                    {
                        c.InCombat = false;
                        c.FightingBoss = false;
                        c.TargetAsleep = false;
                    }
                }

                if (response.ResponseUri.ToString().ToLower().Contains("check"))
                {
                    MessageBox.Show("CHECK FOUND!", "reCAPTCHA", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                else if (c.BreakOnEnchanter && response.ResponseUri.ToString().ToLower().Contains("move") && fullResponse.Contains("<form action = 'index.php?do=enchantitem' method='post'>"))
                {
                    MessageBox.Show("Enchanter Found!", "Enchanter Found!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }

                int len = response.Cookies.Count;
                for (int i = 0; i < len; i++)
                {
                    Cookie rc = response.Cookies[i];
                    bool found = false;
                    for (int j = 0; j < postCookies.Count; j++)
                    {
                        Cookie pc = postCookies[j];
                        if (pc.Name == rc.Name && pc.Path == rc.Path)
                        {
                            found = true;
                            postCookies[j] = rc;
                            break;
                        }
                    }
                    if (!found)
                    {
                        postCookies.Add(rc);
                    }
                }
            }

            if (fullResponse.ToLower().Contains("captcha"))
            {
                MessageBox.Show("CHECK FOUND! - 2", "reCAPTCHA", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }

            int parseStart = 0;
            int parseEnd = 0;

            parseEnd = fullResponse.IndexOf("<a href='index.php?do=buypotions'>") + 70;

            parseStart = fullResponse.IndexOf("(", parseEnd) + 1;
            parseEnd = fullResponse.IndexOf(")", parseStart);
            c.CurrentPotionHPCount = int.Parse(fullResponse.Substring(parseStart, parseEnd - parseStart).Replace(",", ""));

            parseStart = fullResponse.IndexOf("(", parseEnd) + 1;
            parseEnd = fullResponse.IndexOf(")", parseStart);
            c.CurrentPotionMPCount = int.Parse(fullResponse.Substring(parseStart, parseEnd - parseStart).Replace(",", ""));

            parseStart = fullResponse.IndexOf("(", parseEnd) + 1;
            parseEnd = fullResponse.IndexOf(")", parseStart);
            c.CurrentPotionTPCount = int.Parse(fullResponse.Substring(parseStart, parseEnd - parseStart).Replace(",", ""));

            parseStart = fullResponse.IndexOf("<li><a>You own ", parseEnd) + 15;
            parseEnd = fullResponse.IndexOf(" Wood</a></li>", parseStart);
            c.CurrentWoodCount = int.Parse(fullResponse.Substring(parseStart, parseEnd - parseStart).Replace(",", ""));

            parseStart = fullResponse.IndexOf("<li><a>You own ", parseEnd) + 15;
            parseEnd = fullResponse.IndexOf(" Fish</a></li>", parseStart);
            c.CurrentFishCount = int.Parse(fullResponse.Substring(parseStart, parseEnd - parseStart).Replace(",", ""));

            parseStart = fullResponse.IndexOf("<li><a>You own ", parseEnd) + 15;
            parseEnd = fullResponse.IndexOf(" Stone</a></li>", parseStart);
            c.CurrentStoneCount = int.Parse(fullResponse.Substring(parseStart, parseEnd - parseStart).Replace(",", ""));

            parseStart = fullResponse.IndexOf("<li><a>You own ", parseEnd) + 15;
            parseEnd = fullResponse.IndexOf(" Iron</a></li>", parseStart);
            c.CurrentIronCount = int.Parse(fullResponse.Substring(parseStart, parseEnd - parseStart).Replace(",", ""));

            parseEnd = fullResponse.IndexOf("Hello <a href='index.php?do=onlinechar:", parseEnd) + 350;
            parseEnd = fullResponse.IndexOf("<img src=\"images/bars_", parseEnd);
            parseStart = fullResponse.IndexOf("HP: ", parseEnd - 15) + 4;
            c.CurrentHitPoints = int.Parse(fullResponse.Substring(parseStart, parseEnd - parseStart));

            parseEnd = fullResponse.IndexOf("<img src=\"images/bars_", parseEnd + 40);
            parseStart = fullResponse.IndexOf("MP: ", parseEnd - 15) + 4;
            c.CurrentManaPoints = int.Parse(fullResponse.Substring(parseStart, parseEnd - parseStart));

            parseEnd = fullResponse.IndexOf("<img src=\"images/bars_", parseEnd + 40);
            parseStart = fullResponse.IndexOf("TP: ", parseEnd - 15) + 4;
            c.CurrentTravelPoints = int.Parse(fullResponse.Substring(parseStart, parseEnd - parseStart));

            Point loc = new Point();
            parseEnd = fullResponse.IndexOf("<form name=\"navi\" action=\"index.php?do=move\" method=\"post\">", parseEnd + 40);
            parseStart = fullResponse.IndexOf(" at ", parseEnd - 35) + 4;
            parseEnd = fullResponse.IndexOfAny(new char[2] { 'N', 'S' }, parseStart);
            loc.Y = int.Parse(fullResponse.Substring(parseStart, parseEnd - parseStart));
            loc.Y *= (fullResponse[parseEnd] == 'N') ? 1 : -1;
            parseStart = fullResponse.IndexOf(',', parseEnd + 1) + 1;
            parseEnd = fullResponse.IndexOfAny(new char[2] { 'E', 'W' }, parseStart);
            loc.X = int.Parse(fullResponse.Substring(parseStart, parseEnd - parseStart));
            loc.X *= (fullResponse[parseEnd] == 'E') ? 1 : -1;
            c.CurrentLocation = loc;

            if (c.InCombat)
            {
                int sleepStart = parseEnd;
                parseStart = fullResponse.IndexOf(">Health<", parseEnd) + 47;
                if (parseStart >= parseEnd)  //some sort of bug is causing it to think it's in combat when it isn't
                {
                    parseStart = fullResponse.IndexOf('>', parseStart) + 1;
                    parseEnd = fullResponse.IndexOf('<', parseStart + 1);
                    c.TargetHitPoints = int.Parse(fullResponse.Substring(parseStart, parseEnd - parseStart));

                    int tmp = fullResponse.IndexOf(" is asleep.<br", sleepStart, parseStart - sleepStart - 300);
                    if (tmp > 0)
                    {
                        c.TargetAsleep = true;
                    }
                    tmp = fullResponse.IndexOf(" has woken up.<br", sleepStart, parseStart - sleepStart - 300);
                    if (tmp > 0)
                    {
                        c.TargetAsleep = false;
                    }
                }
                else
                {
                    c.InCombat = false;
                    c.TargetAsleep = false;
                    c.FightingBoss = false;
                }
            }

            parseStart = fullResponse.IndexOf("<b>Level</b>: ", parseEnd) + 14;
            parseEnd = fullResponse.IndexOf('<', parseStart);
            c.CurrentLevel = int.Parse(fullResponse.Substring(parseStart, parseEnd - parseStart).Replace(",", ""));
            parseStart = fullResponse.IndexOf("<b>Turns</b>: ", parseEnd) + 14;
            parseEnd = fullResponse.IndexOf('<', parseStart);
            c.CurrentTurns = int.Parse(fullResponse.Substring(parseStart, parseEnd - parseStart).Replace(",", ""));
            parseStart = fullResponse.IndexOf("<b>Gold</b>: ", parseEnd) + 13;
            parseEnd = fullResponse.IndexOf('<', parseStart);
            c.CurrentGold = int.Parse(fullResponse.Substring(parseStart, parseEnd - parseStart).Replace(",", ""));
            parseStart = fullResponse.IndexOf("<b>Bank</b>: ", parseEnd) + 13;
            parseEnd = fullResponse.IndexOf('<', parseStart);
            c.CurrentGoldBank = int.Parse(fullResponse.Substring(parseStart, parseEnd - parseStart).Replace(",", ""));
            parseStart = fullResponse.IndexOf("<b>Dragon Points</b>: ", parseEnd) + 22;
            parseEnd = fullResponse.IndexOf('<', parseStart);
            c.CurrentDragonPoints = int.Parse(fullResponse.Substring(parseStart, parseEnd - parseStart).Replace(",", ""));
            parseStart = fullResponse.IndexOf("<b>DP Bank</b>: ", parseEnd) + 16;
            parseEnd = fullResponse.IndexOf('<', parseStart);
            c.CurrentDPBank = int.Parse(fullResponse.Substring(parseStart, parseEnd - parseStart).Replace(",", ""));

            if (request.RequestUri.Query.ToLower() == "?do=viewvillages")
            {
                int tableStart, tableEnd;
                tableStart = fullResponse.IndexOf("<th>Teleport</th>") + 15;
                tableEnd = fullResponse.IndexOf("</table>", tableStart);

                parseStart = tableStart;
                while (parseStart >= tableStart && parseStart < tableEnd)
                {
                    Village v = new Village();
                    loc = new Point();

                    parseStart = fullResponse.IndexOf("<td>", parseStart) + 4;
                    parseEnd = fullResponse.IndexOf("</td>", parseStart);
                    loc.Y = int.Parse(fullResponse.Substring(parseStart, parseEnd - parseStart));

                    parseStart = fullResponse.IndexOf("<td>", parseStart) + 4;
                    parseEnd = fullResponse.IndexOf("</td>", parseStart);
                    loc.X = int.Parse(fullResponse.Substring(parseStart, parseEnd - parseStart));
                    v.Location = loc;

                    parseStart = fullResponse.IndexOf("<td>", parseStart) + 4;
                    parseEnd = fullResponse.IndexOf("</td>", parseStart);
                    v.Fish = int.Parse(fullResponse.Substring(parseStart, parseEnd - parseStart));

                    parseStart = fullResponse.IndexOf("<td>", parseStart) + 4;
                    parseEnd = fullResponse.IndexOf("</td>", parseStart);
                    v.Wood = int.Parse(fullResponse.Substring(parseStart, parseEnd - parseStart));

                    parseStart = fullResponse.IndexOf("<td>", parseStart) + 4;
                    parseEnd = fullResponse.IndexOf("</td>", parseStart);
                    v.Iron = int.Parse(fullResponse.Substring(parseStart, parseEnd - parseStart));

                    parseStart = fullResponse.IndexOf("<td>", parseStart) + 4;
                    parseEnd = fullResponse.IndexOf("</td>", parseStart);
                    v.Stone = int.Parse(fullResponse.Substring(parseStart, parseEnd - parseStart));

                    c.Villages.Add(v);
                    parseStart = fullResponse.IndexOf("</tr><tr>", parseStart) + 9;
                }

            }
            else if (request.RequestUri.Query.ToLower() == "?do=transfer")
            {
                parseStart = fullResponse.IndexOf("<p class='center'>Summon Troops and Resources from Village</p>") + 63;

                Village v = new Village();
                v.Location = c.CurrentLocation;

                parseStart = fullResponse.IndexOf("<td>Fish</td>", parseStart) + 13;
                parseStart = fullResponse.IndexOf("<td>", parseStart) + 4;
                parseEnd = fullResponse.IndexOf("</td>", parseStart);
                v.Fish = int.Parse(fullResponse.Substring(parseStart, parseEnd - parseStart));

                parseStart = fullResponse.IndexOf("<td>Wood</td>", parseStart) + 14;
                parseStart = fullResponse.IndexOf("<td>", parseStart) + 4;
                parseEnd = fullResponse.IndexOf("</td>", parseStart);
                v.Wood = int.Parse(fullResponse.Substring(parseStart, parseEnd - parseStart));

                parseStart = fullResponse.IndexOf("<td>Stone</td>", parseStart) + 15;
                parseStart = fullResponse.IndexOf("<td>", parseStart) + 4;
                parseEnd = fullResponse.IndexOf("</td>", parseStart);
                v.Stone = int.Parse(fullResponse.Substring(parseStart, parseEnd - parseStart));

                parseStart = fullResponse.IndexOf("<td>Iron</td>", parseStart) + 14;
                parseStart = fullResponse.IndexOf("<td>", parseStart) + 4;
                parseEnd = fullResponse.IndexOf("</td>", parseStart);
                v.Iron = int.Parse(fullResponse.Substring(parseStart, parseEnd - parseStart));

                parseStart = fullResponse.IndexOf("<td>Gold</td>", parseStart) + 14;
                parseStart = fullResponse.IndexOf("<td>", parseStart) + 4;
                parseEnd = fullResponse.IndexOf("</td>", parseStart);
                v.Gold = int.Parse(fullResponse.Substring(parseStart, parseEnd - parseStart));

                parseStart = fullResponse.IndexOf("<td>DP</td>", parseStart) + 12;
                parseStart = fullResponse.IndexOf("<td>", parseStart) + 4;
                parseEnd = fullResponse.IndexOf("</td>", parseStart);
                v.DragonPoints = int.Parse(fullResponse.Substring(parseStart, parseEnd - parseStart));

                c.CurrentVillage = v;
            }

            return c;
        }