以编程方式登录facebook [英] Log into facebook programatically

查看:94
本文介绍了以编程方式登录facebook的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我正在尝试使用HttpWebRequest以编程方式登录facebook,但我似乎无法正确使用

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

我得到的回应就是" ; 必须在您的浏览器中启用Cookie "

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();
        }
    }
}

推荐答案

您正在尝试使用API​​来调用UI页面。这不会有效,因为"UI"上没有任何内容。 side将执行诸如脚本或cookie。而是使用Facebook
的REST API
公开 为此,你很可能想要使用HttpClient,因为它处理了你必须为WebRequest编写的许多代码。

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.

如果你真的需要HTML那么您可以将WebRequest与Cookie一起使用,但这仅限于您需要原始HTML。请注意检查"cookies"的应用程序将通过运行脚本来执行此操作,但该脚本不会通过WebRequest
逻辑运行。运行该脚本逻辑的唯一方法是在WebBrowser中托管页面,但WebBrowser是IE的真正旧版本,因此该站点甚至可能无法正确加载。

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你的代码,你正在使用cookie。您正在提出几个请求,因此不清楚哪个请求失败了。在第一个中,您将cookie容器创建为临时变量。然后创建请求对象,
为其分配一个全新的cookie容器(与temp变量无关)。不久之后,您将使用响应中的容器清除cookie容器。然后,您将开始一个新请求并创建一个新容器。然后将最后一个响应中的cookie
复制到新请求中。创建cookie容器一次并将其分配给请求。在后续通话中重复使用该请求,您不必对cookie执行任何操作。

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.


这篇关于以编程方式登录facebook的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆