如何在.NET中没有浏览器的情况下保持facebook登录?

时间:2011-04-03 22:01:54

标签: .net facebook

我使用.NET中的HTTPWebRequest登录到facebook,它可以正常返回home.php登录,但如果我尝试加载另一个页面,则不会登录。

我已经尝试从主页回复中保存cookie但仍然无效。

任何帮助都将不胜感激。

我有以下代码:

try
    {
        //Get Cookies
        strURL = "https://login.facebook.com/login.php";
        request =(HttpWebRequest) HttpWebRequest.Create(strURL);
        request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.204 Safari/534.16";


        request.Method = "GET";
        request.CookieContainer = cookieJar;
        response = (HttpWebResponse)request.GetResponse();

        foreach (System.Net.Cookie tempCookie in response.Cookies)
        {
            cookieJar.Add(tempCookie);
        }

        //Send the post data now
        request = (HttpWebRequest)HttpWebRequest.Create(strURL);
        request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.204 Safari/534.16";

        string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
        request.Method = "POST";
        request.AllowAutoRedirect = true;
        request.CookieContainer = cookieJar;
        request.ContentType = "multipart/form-data; boundary=" + boundary;
        request.KeepAlive = true;
        StreamWriter writer = new StreamWriter(request.GetRequestStream());
        writer.Write("email=xxx@gmail.com&pass=xxx&persistent_inputcheckbox=on");
        writer.Close();
        response = (HttpWebResponse)request.GetResponse();
        CookieContainer loggedInCookies = new CookieContainer();
        foreach (Cookie tempCookie in response.Cookies)
        {
            loggedInCookies.Add(tempCookie);

        }
        //Get the data from the page
        StreamReader stream = new StreamReader(response.GetResponseStream());
        string data = stream.ReadToEnd();
        response.Close();

        if (data.Contains("<title>Facebook") == true)
        {
            //LOGGED IN SUCCESSFULLY
        }
        return loggedInCookies;

    }
    catch (Exception ex)
    {
        return null;
    } 

0 个答案:

没有答案