title: Facebook Captcha
body: <p>I'm trying to get the facebook captcha image in c# and then display it in my app and send the response back to facebook. My app works fine which basically communicates with facebook but after a while it asks for captcha. I incorporated the functionality for entering the captcha and when I send it to facebook using webclient I get the response that it worked. But when I again try to query the data I'm again directed to the captcha link and the cycle continues.</p>
  
  <p>I just can't figure out how I attach the information in my subsequent requests that I have already entered captcha or what information returned from the captcha response header I should attach to my new requests.</p>
  
  <p>Sorry for my poor English. I hope I conveyed the message.</p>
  
  <pre><code>    private bool checkNumber(string number)
      {
          int index = 0;
          StreamReader streamReader;
          NameValueCollection nameValuePairs = new NameValueCollection();
          nameValuePairs.Add("lsd", "AVoA7hUw");
          nameValuePairs.Add("email", number);
          nameValuePairs.Add("did_submit", "Search");
          nameValuePairs.Add("__user", "0");
          nameValuePairs.Add("__a", "1");
          nameValuePairs.Add("__dyn", "7w");
          nameValuePairs.Add("__req", "1");
          nameValuePairs.Add("fb_dtsg", "AQCHtLfI");
          nameValuePairs.Add("phstamp", "1658167721167610273109");
  
  
          WebClient client = new WebClient();
          byte[] response = client.UploadValues("https://www.facebook.com/ajax/login/help/identify.php?ctx=recover", nameValuePairs);
          char[] chars = new char[response.Length];
          for (int i = 0; i &lt; response.Length; i++)
          {
              chars[i] = Convert.ToChar(response[i]);
          }
          string s = new string(chars);
          if (s.Contains("onload"))
          {
              String coo = "";
  
              CookieContainer cookies = new CookieContainer();
              String[] cookieString = client.ResponseHeaders.Get("set-cookie").Split(';');
  
              string name = cookieString[0].Substring(0, cookieString[0].IndexOf("=")).TrimStart();
              string value = cookieString[0].Substring(cookieString[0].IndexOf("=") + 1).TrimEnd(';');
              Cookie cookie = new Cookie(name, value);
              cookie.Domain = "www.facebook.com";
              cookies.Add(cookie);
              coo += name + "=" + value + ";";
  
              Cookie newCookie = new Cookie("reg_fb_gate", "https%3A%2F%2Fwww.facebook.com%2F");
              newCookie.Domain = "www.facebook.com";
              cookies.Add(newCookie);
              newCookie = new Cookie("reg_fb_ref", "https%3A%2F%2Fwww.facebook.com%2Flogin%2Fidentify%3Fctx%3Drecover");
              newCookie.Domain = "www.facebook.com";
  
              coo += "reg_fb_gate=https%3A%2F%2Fwww.facebook.com%2F;reg_fb_ref=https%3A%2F%2Fwww.facebook.com%2Flogin%2Fidentify%3Fctx%3Drecover;";
              cookies.Add(newCookie);
  
              name = cookieString[4].Substring(10, cookieString[4].LastIndexOf("=") - 10).TrimStart();
              value = cookieString[4].Substring(cookieString[4].IndexOf("=") + 1).TrimEnd(';');
              cookie = new Cookie(name, value);
              cookie.Domain = "www.facebook.com";
              cookies.Add(cookie);
              coo += name + "=" + value + ";";
  
  
  
  
  
  
              s = "";
              HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://www.facebook.com/recover/initiate");
  
              //req.Headers.Add("set-cookie", cookie);
              req.Host = "www.facebook.com";
              req.CookieContainer = cookies;
              req.Method = "GET";
              req.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:22.0) Gecko/20100101 Firefox/22.0";
              req.KeepAlive = true;
              WebResponse resp = req.GetResponse();
              streamReader = new StreamReader(resp.GetResponseStream());
              s = streamReader.ReadToEnd().Trim();
              if (s.Contains("sometext"))
              {
                  return true;
              }
              while (s.Contains("captcha") || index == -1)
              {
                  req = (HttpWebRequest)WebRequest.Create("https://www.facebook.com/recover/initiate");
                  req.Host = "www.facebook.com";
                  req.CookieContainer = cookies;
                  req.Method = "GET";
                  req.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:22.0) Gecko/20100101 Firefox/22.0";
                  req.KeepAlive = true;
                  resp = req.GetResponse();
                  streamReader = new StreamReader(resp.GetResponseStream());
                  s = streamReader.ReadToEnd().Trim();
  
                  checkBox1.IsChecked = false;
  
                  index = s.IndexOf("https://www.facebook.com/captcha/tfbimage.php?");
                  if (index == -1)
                      continue;
                  string uri = s.Substring(index);
                  uri = uri.Substring(0, uri.IndexOf("\\\"") - 1);
  
                  string captcha_persist_data = s.Substring(s.IndexOf("captcha_persist_data"));
                  captcha_persist_data = captcha_persist_data.Substring(s.IndexOf("value=\\\"") + 7);
                  captcha_persist_data = captcha_persist_data.Substring(0, s.IndexOf("\\\"") - 1);
                  client = new WebClient();
                  coo += "wd=1920x976;act=1374854201395/3;_e_0oL1_0=%5B%220oL1%22%2C1375031079666%2C%22act%22%2C1375031079665%2C0%2C%22captcha_response%22%2C%22click%22%2C%22click%22%2C%22-%22%2C%22r%22%2C%22%2Frecover%2Finitiate%22%2C%7B%22ft%22%3A%7B%7D%2C%22gt%22%3A%7B%7D%7D%2C0%2C0%2C0%2C1903%2C16%5D;_e_0oL1_1=%5B%220oL1%22%2C1375031092251%2C%22act%22%2C1375031092250%2C1%2C%22captcha_submit%22%2C%22click%22%2C%22click%22%2C%22-%22%2C%22r%22%2C%22%2Frecover%2Finitiate%22%2C%7B%22ft%22%3A%7B%7D%2C%22gt%22%3A%7B%7D%7D%2C0%2C0%2C0%2C1903%2C16%5D";
                  client.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:22.0) Gecko/20100101 Firefox/22.0");
                  client.Headers.Add("host", "www.facebook.com");
                  client.Headers.Add("set-cookie", coo);                    
  
                  image1.Source = new BitmapImage(new Uri(uri));
                  MessageBox.Show("Alert");
                  streamReader = new StreamReader("D:\\abc.txt");
                  string newStr = streamReader.ReadLine();
                  streamReader.Close();
  
  
  
                  nameValuePairs = new NameValueCollection();
                  nameValuePairs.Add("lsd", "AVoA7hUw");
                  /*nameValuePairs.Add("email", number);
                  nameValuePairs.Add("did_submit", "Search");
                  nameValuePairs.Add("__user", "0");
                  nameValuePairs.Add("__a", "1");
                  nameValuePairs.Add("__dyn", "7w");
                  nameValuePairs.Add("__req", "1");
                  nameValuePairs.Add("fb_dtsg", "AQCHtLfI");
                  nameValuePairs.Add("ttstamp", "26581656790668586");  */
                  nameValuePairs.Add("captcha_persist_data", captcha_persist_data);
                  //nameValuePairs.Add("recaptcha_challenge_field", "");                  
  
                  nameValuePairs.Add("captcha_response", newStr);
                  //nameValuePairs.Add("confirmed", "1");
                  nameValuePairs.Add("captcha_submit", "Submit");
  
                  response = client.UploadValues("https://www.facebook.com/recover/initiate", nameValuePairs);
                  chars = new char[response.Length];
                  for (int i = 0; i &lt; response.Length; i++)
                  {
                      chars[i] = Convert.ToChar(response[i]);
                  }
                  s = new string(chars);
  
                  cookies = new CookieContainer();
  
                  coo = "";                    
                  cookieString = client.ResponseHeaders.Get("set-cookie").Split(';');
  
                  name = cookieString[0].Substring(0, cookieString[0].IndexOf("=")).TrimStart();
                  value = cookieString[0].Substring(cookieString[0].IndexOf("=") + 1).TrimEnd(';');
                  cookie = new Cookie(name, value);
                  cookie.Domain = "www.facebook.com";
                  cookies.Add(cookie);
                  coo += name + "=" + value + ";";
  
                  newCookie = new Cookie("reg_fb_gate", "https%3A%2F%2Fwww.facebook.com%2F");
                  newCookie.Domain = "www.facebook.com";
                  cookies.Add(newCookie);
                  newCookie = new Cookie("reg_fb_ref", "https%3A%2F%2Fwww.facebook.com%2Flogin%2Fidentify%3Fctx%3Drecover");
                  newCookie.Domain = "www.facebook.com";
                  coo += "reg_fb_gate=https%3A%2F%2Fwww.facebook.com%2F;reg_fb_ref=https%3A%2F%2Fwww.facebook.com%2Flogin%2Fidentify%3Fctx%3Drecover;";
  
  
                  cookies.Add(newCookie);
  
                  name = cookieString[4].Substring(10, cookieString[4].LastIndexOf("=") - 10).TrimStart();
                  value = cookieString[4].Substring(cookieString[4].IndexOf("=") + 1).TrimEnd(';');
                  cookie = new Cookie(name, value);
                  cookie.Domain = "www.facebook.com";
                  cookies.Add(cookie);
                  coo += name + "=" + value + ";";
                  coo+= "highContrastMode=0;wd=1920x976;";
  
                  nameValuePairs = new NameValueCollection();
                  nameValuePairs.Add("__user", "0");
                  nameValuePairs.Add("__a", "1");
                  nameValuePairs.Add("__dyn", "7w86i");
                  nameValuePairs.Add("__req", "1");
                  nameValuePairs.Add("fb_dtsg", "AQDdK6At");
                  nameValuePairs.Add("q", "Miny1~66~%22%3A~%22%3A%22~%2C%22~%22%2C%22~.~null~0~%22%2C%7B%22~script_path_change~%5D%2C%5B%22~%22%3A%5B~time_spent~1~user~page_id~97dbjl~trigger~time~posts~%22%3A%5B%5B%22~source_path~WebRecoverInitiateController~source_token~01afab33~dest_path~dest_token~navigation~cause~%22%7D%2C~29183~time_spent_bit_array~tos_id~start_time~tos_array~%2C~%5D%2C%22~tos_len~tos_seq~tos_cum~%7D%2C~noxdt2~ods~%3A~ms~qa~www~15~4~%5B%7B%22~1375031063833~unload~1375031063~1053788547~31~21~%5D%5D%7D%2C%7B%22~1375031093606~load~1375031093~11~9597~bits~js_initialized~%5D%7D%2C~10599~%5D%5D%7D%5D~1MJxCzKxLzMxEzNw1NyOPEDQxRzSxTzUwByVwByWwByXx1OYZF-D_xLz1ww1Py1xG1Q1yC1z1Aw1Ry1BwIy1Cw1S1DZ1TJxCzKx1EzMx1F1G1HAHA1IA1JzNw1UyOPEDQwBySwByUxRzVxTzWwByXx1VY1KF-D_x1Ez1ww1Wy1xG1K1yC1z1Aw1Xy1BwCy1Cw1L1D1YF1F1G1HAHA1IA1JDHA1ZA1-GI1_2w2x");  
                  nameValuePairs.Add("ts", "1375031104207");
                  nameValuePairs.Add("ph", "V3");     
                  nameValuePairs.Add("miny_encode_ms", "6");
  
                  client = new WebClient();
                  client.Headers.Add("set-cookie", coo);
                  response = client.UploadValues("https://www.facebook.com/ajax/bz", nameValuePairs);
                  chars = new char[response.Length];
                  for (int i = 0; i &lt; response.Length; i++)
                  {
                      chars[i] = Convert.ToChar(response[i]);
                  }
                  s = new string(chars);
  
  
                  req = (HttpWebRequest)WebRequest.Create("https://www.facebook.com/recover/initiate");
  
  
                  //req.Headers.Add("set-cookie", cookie);
                  req.Host = "www.facebook.com";
                  req.CookieContainer = cookies;
                  req.Method = "GET";
                  req.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:22.0) Gecko/20100101 Firefox/22.0";
                  req.KeepAlive = true;
                  resp = req.GetResponse();
                  streamReader = new StreamReader(resp.GetResponseStream());
                  s = streamReader.ReadToEnd().Trim();
                  if (s.Contains("sometext"))   //example text to look for in the response
                  {
                      return true;
                  }
  
              }
  
  
          }
          return false;
  </code></pre>