0

我有一些代码可以将我登录到 Facebook,它可以正常工作,因为它可以在您登录后立即返回您看到的页面。但我试图在登录后请求我的实际个人资料,而不仅仅是我的新闻提要。所以我必须将我的电子邮件和密码发送到登录页面,然后请求我的个人资料。在请求我的个人资料时如何保留登录数据?

这是我所拥有的

    public static string logIn()
    {
        //get the cookies before you try to log in
        CookieCollection cookies = new CookieCollection();
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.facebook.com"); 
        request.CookieContainer = new CookieContainer();
        request.CookieContainer.Add(cookies);
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        cookies = response.Cookies;
        response.Close();

        //logging in
        HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create("https://www.facebook.com/login.php?login_attempt=1");
        getRequest.CookieContainer = new CookieContainer();
        getRequest.CookieContainer.Add(cookies);
        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";

        //sending the email/password
        byte[] byteArray = Encoding.ASCII.GetBytes("email=myemail@yahoo.com&pass=mypassword");
        getRequest.ContentLength = byteArray.Length;
        Stream newStream = getRequest.GetRequestStream();
        newStream.Write(byteArray, 0, byteArray.Length); 
        newStream.Close();

        //returns the source of the page after logging in
        HttpWebResponse getResponse = (HttpWebResponse)getRequest.GetResponse();
        StreamReader sr = new StreamReader(getResponse.GetResponseStream());
        string source = sr.ReadToEnd();
        cookies.Add(getResponse.Cookies);

        //tries to get my profile source
        //everything works fine until here
        getRequest = (HttpWebRequest)WebRequest.Create("http://www.facebook.com/myprofile");
        getRequest.CookieContainer = new CookieContainer();
        getRequest.CookieContainer.Add(cookies);
        getRequest.Method = WebRequestMethods.Http.Get;
        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";

        getResponse = (HttpWebResponse)getRequest.GetResponse();
        sr = new StreamReader(getResponse.GetResponseStream());
        source = sr.ReadToEnd();
        getResponse.Close();
        return source;
    }

我已经尝试了几种方法,我已经让它返回我的个人资料,但它返回它就好像我没有登录并且你实际上无法查看我的个人资料(因为它被设置为私人)所以我需要在请求我的个人资料时以某种方式包含我的登录信息。

4

2 回答 2

0

当我在做类似的事情时,这让我难倒了一段时间。登录后,您需要确保为您的请求设置了正确的 cookie。在将新 cookieCookieCollection应用到上一个请求之前,尝试将它们添加到对象中。

// ^ previous code ^
HttpWebResponse getResponse = (HttpWebResponse)getRequest.GetResponse();
StreamReader sr = new StreamReader(getResponse.GetResponseStream());
string source = sr.ReadToEnd();

Uri facebookPage = new Uri("http://www.facebook.com"); // .GetCookies() only accepts a Uri
cookies.Add(request.CookieContainer.GetCookies(facebookPage));

getResponse.Close(); // Always make sure you close your responses

// V requesting your profile code V
于 2012-07-22T00:12:49.800 回答
0

您必须在开始时启动 CookieCollection 并将其用于所有请求。每当您发出请求时,CookieCollection 都会使用新的 cookie 更新自身,因此使用 cookies.Add() 也会将其搞砸。但我现在可以正常工作了。

于 2012-07-24T21:33:26.803 回答