none
Log into facebook programatically RRS feed

  • Question

  • hello everyone, i'm trying to log into facebook programatically using the HttpWebRequest, but i can't seem to get it right

    the response i get is that "the cookies must be enabled in your browser"

    below is the code i'm using, would please help me to find out what is wrong, thank you in advance :

    namespace LOGIN
    {
        class Program
        {
            static void Main(string[] args)
            {
                CookieCollection cookies = new CookieCollection();
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.facebook.com"); 
               
                request.CookieContainer = new CookieContainer();
                
                //Get the response from the server and save the cookies from the first request..
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                cookies = response.Cookies;
    
                string getUrl = "https://www.facebook.com/login.php?login_attempt=1";
                string postData = String.Format("email={0}&pass={1}", "myemail", "mypassword");
                HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create(getUrl);
                getRequest.CookieContainer = new CookieContainer();
                getRequest.CookieContainer.Add(cookies); //recover cookies First request
                getRequest.Method = WebRequestMethods.Http.Post;
                getRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.79 Safari/537.36";
                getRequest.AllowWriteStreamBuffering = true;
                getRequest.ProtocolVersion = HttpVersion.Version11;
                getRequest.AllowAutoRedirect = true;
                getRequest.ContentType = "application/x-www-form-urlencoded";
    
                byte[] byteArray = Encoding.ASCII.GetBytes(postData);
                getRequest.ContentLength = byteArray.Length;
                Stream newStream = getRequest.GetRequestStream(); //open connection
                newStream.Write(byteArray, 0, byteArray.Length); // Send the data.
                newStream.Close();
    
                HttpWebResponse getResponse = (HttpWebResponse)getRequest.GetResponse();
                using (StreamReader sr = new StreamReader(getResponse.GetResponseStream()))
                {
                    Console.WriteLine(sr.ReadToEnd());
                    File.WriteAllText(@"C:\Users\test\Desktop\source.html", sr.ReadToEnd());
                    
                }
                Console.Read();
            }
        }
    }

    Wednesday, June 13, 2018 11:06 AM

All replies

  • You're trying to use an API to call a UI page. That isn't going to work out because nothing on the "UI" side will execute such as scripts or cookies. Instead use the REST API that Facebook exposes.  For that you'll most likely want to use HttpClient as it handles a lot of the code you'd have to write for WebRequest.

    If you really need the HTML then you could use WebRequest with cookies but that is only if you want the raw HTML. Be aware though that apps that check for "cookies" will do so by running a script but that script won't run via the WebRequest logic. The only way to run that scripting logic would be to host the page in WebBrowser but WebBrowser is a really old version of IE so the site may not even load properly.

    In your code that you're bouncing around with the cookies. You're making a couple of requests so it is unclear which request is failing. In your first one you create a cookie container into a temp variable. Then you create the request object and assign it a brand new cookie container (unrelated to your temp variable). A little while later you wipe out your cookie container with the container from the response. Then later on you start a new request and create a new container. Then you copy the cookies from the last response into the new request. Create the cookie container once and assign it to the request. Reuse the request on subsequent calls and you shouldn't have to do anything with the cookies.



    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, June 13, 2018 1:44 PM
    Moderator
  • 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
    Wednesday, June 13, 2018 2:39 PM
  • Cookies by themselves do nothing. Based upon your description that would mean that your second request to Facebook is failing because you're not passing back the cookie they expect. In that case look at the cookie container you're getting from them and ensure that it is being added as part of the second request.

    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, June 13, 2018 3:02 PM
    Moderator
  • I'm passing back the cookies that facebook expects, cause I stored the cookies from the first request inside a CookierCollection which later I assigned it to the CookieContainer of the second request ! is there anything wrong with this ? 
    Wednesday, June 13, 2018 3:23 PM
  • As I mentioned, you're creating more containers then you probably need. Since I cannot run your code I cannot verify the container is correct but you can use the debugger to check it. In your second request you create a new container and then assign the cookies from the first container to it. After that line of code, check the container to ensure the cookies that you expected are there. Note that modern apps tend to try to lock down cookies as they can be a security issue. So you might not even be getting the cookie back from FB to begin with. Verify that you are.

    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, June 13, 2018 3:41 PM
    Moderator