我正在尝试在c#中获取facebook验证码图像,然后将其显示在我的应用中并将响应发送回Facebook。我的应用程序工作正常,基本上与Facebook通信,但过了一段时间,它要求验证码。我合并了输入验证码的功能,当我使用webclient将它发送到Facebook时,它只是在响应中感知另一个验证码。我已经使用TemperData inn Firefox进行了双重检查我正在设置正确的标题信息和所有字段,但我仍然没有被重定向到正确的页面。事实上,在响应头中,我可以看到一个字段与TemperData相比缺失。
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 Firefenter code hereox/22.0";
req.KeepAlive = true;
resp = req.GetResponse();
streamReader = new StreamReader(resp.GetResponseStream());
s = streamReader.ReadToEnd().Trim();
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(captcha_persist_data.IndexOf("value") + 7);
captcha_persist_data = captcha_persist_data.Substring(0, 1156);
string lsd = s.Substring(s.IndexOf("lsd"));
lsd = lsd.Substring(lsd.IndexOf("value") + 7);
lsd = lsd.Substring(0, 8);
client = new WebClient();
coo += "act=1375466977443%2F3;_e_0p4M_2=%5B%220p4M%22%2C1375466960620%2C%22act%22%2C1375466960619%2C2%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%2C1423%2C16%5D;wd=1920x796;_e_0p4M_3=%5B%220p4M%22%2C1375466977445%2C%22act%22%2C1375466977443%2C3%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%2C1423%2C16%5D";
client.Headers[HttpRequestHeader.Host] = "www.facebook.com";
client.Headers[HttpRequestHeader.UserAgent] = "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:22.0) Gecko/20100101 Firefox/22.0";
client.Headers[HttpRequestHeader.Accept] = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
client.Headers[HttpRequestHeader.AcceptLanguage] = "en-US,en;q=0.5";
//client.Headers[HttpRequestHeader.AcceptEncoding] = "gzip, deflate";
client.Headers[HttpRequestHeader.Referer]="https://www.facebook.com/recover/initiate";
client.Headers[HttpRequestHeader.Cookie] = coo;
//client.Headers[HttpRequestHeader.Connection] = "keep-alive";
client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
//client.Headers[HttpRequestHeader.ContentLength] = "1235";
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", lsd);
nameValuePairs.Add("captcha_persist_data", captcha_persist_data);
nameValuePairs.Add("captcha_response", newStr);
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);
非常感谢任何帮助。