I'm trying to login to Facebook in my program and parse some information from there (like name, profile pic, etc).
I'm getting redirected back to Facebook's main page everytime I execute the code below.
string email = "email";
string pw = "pw";
string PostData = String.Format("email={0}&pass={1}", email, pw);
CookieContainer cookieContainer = new CookieContainer();
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("");
req.CookieContainer = cookieContainer;
req.Method = "POST";
req.ContentLength = PostData.Length;
req.ContentType = "application/x-www-form-urlencoded";
req.AllowAutoRedirect = true;
req.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] loginDataBytes = encoding.GetBytes(PostData);
req.ContentLength = loginDataBytes.Length;
Stream stream = req.GetRequestStream();
stream.Write(loginDataBytes, 0, loginDataBytes.Length);
HttpWebResponse webResp = (HttpWebResponse)req.GetResponse();
Stream datastream = webResp.GetResponseStream();
StreamReader reader = new StreamReader(datastream);
webBrowser1.DocumentText = reader.ReadToEnd();
foreach (Cookie cookies in webResp.Cookies)
{
MessageBox.Show(cookies.Name + " " + cookies.Value);
}
What am I doing wrong here? Any help would be appreciated, many thanks! :)
edit: I found out how to do it shortly after I posted.
Facebook sends a cookie everytime you visit it to see if you have cookies enabled, what I did was send a request to the login page of Facebook to get the cookies, then send another with the POST data. It worked this way and I successfully logged in.
Thanks anyway! :)