我正在尝试在 c# 中获取 facebook 验证码图像,然后将其显示在我的应用程序中并将响应发送回 facebook。我的应用程序运行良好,基本上与 facebook 通信,但过了一段时间它要求验证码。我整合了输入验证码的功能,当我使用 webclient 将其发送到 facebook 时,我得到了它有效的响应。但是当我再次尝试查询数据时,我再次被定向到验证码链接并且循环继续。
我只是不知道如何在我已经输入验证码的后续请求中附加信息,或者我应该将验证码响应标头返回的信息附加到我的新请求中。
对不起我的英语不好。我希望我传达了信息。
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 < 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 < 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 < 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;