1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Facebook Login issue C#

Discussion in 'C, C++, C#' started by masush300, Nov 11, 2015.

  1. masush300

    masush300 Jr. VIP Jr. VIP Premium Member

    Joined:
    Mar 16, 2012
    Messages:
    1,091
    Likes Received:
    344
    Occupation:
    Coding
    Location:
    Imagine Dragons
    Home Page:
    Hi everyone,

    Recently i made a simple software using c# http request but now it seems there is a problem logging in facebook getting and sending request

    here is few lines of code that used work
    Code:
    string email = "email";string pw = "pw";string PostData = String.Format("email={0}&pass={1}", email, pw);
    
    
    
    
                 CookieCollection cookies = new CookieCollection();
     HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.facebook.com"); 
     request.CookieContainer = new CookieContainer();
     request.CookieContainer.Add(cookies);
     //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}",  "[email protected]", "yourpassword");
     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 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
     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()))
     {
       string sourceCode = sr.ReadToEnd();
       webBrowser1.ScriptErrorsSuppressed = true;
       webBrowser1.DocumentText = sourceCode;   
     
     }

    First ,saving cookies after sending request then using it to login on facebook.(so that it won't mention "Please enable cookies...")

    and Now it doesn't work

    Anyone know the issue or should i switch to httpclient (though i haven't used httpclient much)

    thanks in advance
     
  2. weedsmoker

    weedsmoker Junior Member

    Joined:
    May 2, 2011
    Messages:
    190
    Likes Received:
    79
    I just gave it a quick glance, so maybe I'm wrong, there is some js generated token value which is needed for logging. FB was bitch before because of too much js, so I used only fb mobile version for http requests, I assume situation now is probably worse, maybe try logging in through their mobile site?
     
  3. rootjazz

    rootjazz Jr. VIP Jr. VIP

    Joined:
    Dec 21, 2012
    Messages:
    695
    Likes Received:
    340
    Occupation:
    Developer
    Location:
    UK
    Home Page:
    check the headers you send vs what happens in the browser with fiddler2

    the answer is in the headers - always
     
  4. masush300

    masush300 Jr. VIP Jr. VIP Premium Member

    Joined:
    Mar 16, 2012
    Messages:
    1,091
    Likes Received:
    344
    Occupation:
    Coding
    Location:
    Imagine Dragons
    Home Page:
    It seems like they aren't sending cookies at first attempt maybe i am missing something. thanks for your advise I will try using their mobile site.

    I have never tried fiddler2 but have tried using wireshark but it doesn't seems to capture https. Does fiddler2 works with https.