dima shawahneh -1 Light Poster

Hello every one,
I am trying to login to Facebook in a programmatic way.
i.e. I want to send user name and password within an http request and response.

The problem I faced :
Cookies are not enabled on your browser. Please adjust this in your security preferences before continuing.

But, Cookies are enabled in the browser since I can login to the site using the browser.
I tried IE,Firefox,Google Chrome browsers.. all give the same result!!

Here is the code i use ..

void login()
        {
            string m_apiKey = "70861738083364624c408805d3de4bde";
            string email ="myemail@hotmail.com";
            string pass = "****";

            NetworkCredential credentials = new NetworkCredential(email,pass);
            CookieContainer cookies = new CookieContainer();

            string data = "username=" + email + "&password=" + pass;
            string webpage = HttpPostRedirect("https://www.facebook.com/login.php?  api_key=" + m_apiKey + "&v=1.0&next=http://www.facebook.com/connect/login_success.html&cancel_url=http://www.facebook.com/connect/login_failure.html&fbconnect=true&return_session=true", data, credentials, cookies);
          
        }


        // HTTP POST REDIRECT METHOD
        protected string HttpPostRedirect(string uri, string data,NetworkCredential networkCredentials, CookieContainer cookies)
        {
           
            // PREPARE REQUEST
            System.Net.HttpWebRequest req =(HttpWebRequest)System.Net.WebRequest.Create(uri);
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
            req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT5.1; SV1; .NET CLR 2.0.50727)";
            req.KeepAlive = true;
            req.CookieContainer = cookies;
            req.Credentials = networkCredentials;
            req.AllowAutoRedirect = false;
            req.AllowWriteStreamBuffering = false;

            // ENCODE DATA & POST IT
            byte[] bytes = System.Text.Encoding.ASCII.GetBytes(data);
            req.ContentLength = bytes.Length;
            System.IO.Stream os = req.GetRequestStream();
            os.Write(bytes, 0, bytes.Length);
            os.Close();

            // GET RESPONSE
            System.Net.HttpWebResponse resp =(HttpWebResponse)req.GetResponse();
            if (resp == null) return null;

            // SAVE COOKIES
            cookies.Add(req.RequestUri, resp.Cookies);

            //resp.Headers["Set-Cookie"];
            // 302 REDIRECT?
            string webpage;
            if (resp.StatusCode == HttpStatusCode.Found)
            {
               // ...
               webpage = "dima";
               
            }
            else
            {
              // READ RESPONSE
              StreamReader sr = new StreamReader(resp.GetResponseStream());
              webpage = sr.ReadToEnd().Trim();
              WriteLinesToFile(@"C:\Users\v-dimas\Desktop\test2.html", webpage);
            }
            return webpage;
        }

        public void WriteLinesToFile(string filePath, string data)
        {
            if (filePath == null || filePath.Length == 0)
                return;
            if (data == null)
                return;

            StreamWriter fileWriter = null;
            try
            {
                if (File.Exists(filePath))
                    fileWriter = File.AppendText(filePath);
                else
                    fileWriter = File.CreateText(filePath);

                //foreach (string line in lines)
                    fileWriter.Write(data);
            }
            finally
            {
                if (fileWriter != null)
                    fileWriter.Close();
            }
        }

Any suggestions would be appreciated ..
thanks in advance

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.