C# - HttpWebRequest POST(登录到 Facebook) [英] C# - HttpWebRequest POST (Login to Facebook)

查看:50
本文介绍了C# - HttpWebRequest POST(登录到 Facebook)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的程序中登录 Facebook 并从中解析一些信息(例如姓名、个人资料图片等).

I'm trying to login to Facebook in my program and parse some information from there (like name, profile pic, etc).

每次执行下面的代码时,我都会被重定向回 Facebook 的主页.

I'm getting redirected back to Facebook's main page everytime I execute the code below.

string email = "email";
string pw = "pw";
string PostData = String.Format("email={0}&pass={1}", email, pw);

CookieContainer cookieContainer = new CookieContainer();

HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("");
req.CookieContainer = cookieContainer;
req.Method = "POST";
req.ContentLength = PostData.Length;
req.ContentType = "application/x-www-form-urlencoded";
req.AllowAutoRedirect = true;
req.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";

ASCIIEncoding encoding = new ASCIIEncoding();
byte[] loginDataBytes = encoding.GetBytes(PostData);
req.ContentLength = loginDataBytes.Length;
Stream stream = req.GetRequestStream();
stream.Write(loginDataBytes, 0, loginDataBytes.Length);

HttpWebResponse webResp = (HttpWebResponse)req.GetResponse();

Stream datastream = webResp.GetResponseStream();
StreamReader reader = new StreamReader(datastream);
webBrowser1.DocumentText = reader.ReadToEnd();

foreach (Cookie cookies in webResp.Cookies)
{
    MessageBox.Show(cookies.Name + "   " + cookies.Value);
}

我在这里做错了什么?任何帮助将不胜感激,非常感谢!:)

What am I doing wrong here? Any help would be appreciated, many thanks! :)

发布后不久我就知道了如何去做.

edit: I found out how to do it shortly after I posted.

Facebook 每次访问时都会发送一个 cookie 以查看是否启用了 cookie,我所做的是向 Facebook 的登录页面发送请求以获取 cookie,然后发送另一个带有 POST 数据的请求.它以这种方式工作,我成功登录.

Facebook sends a cookie everytime you visit it to see if you have cookies enabled, what I did was send a request to the login page of Facebook to get the cookies, then send another with the POST data. It worked this way and I successfully logged in.

还是谢谢!:)

推荐答案

很高兴你找到了答案,我也可以使用 HttpWebRequest 登录 facebook,就像你说的一样.. 这是一个可以接受的解决方法:

I'm glad you found your answer, I could login facebook using HttpWebRequest too, just like you said.. Here's an acceptable workaround:

第一个请求:获取 cookie.

First request: Get the cookies.

 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;

第二个请求: POST 表单数据并从第一个请求中恢复 cookie..

Second request: POST the form data and recover the cookies from the first request..

 string getUrl = "https://www.facebook.com/login.php?login_attempt=1";
 string postData = String.Format("email={0}&pass={1}", "value1", "value2");
 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();               
 } 

这是与 HttpWebRequest 一起使用的众多方法之一,此外,如果您更喜欢使用 WebBrowser,它可以使用以下方法工作:

This is one of lots ways that works with HttpWebRequest, also if you prefer using WebBrowser, it works acceptable using the following:

webBrowser1.Navigate("https://www.facebook.com/login.php?login_attempt=1", "",byteArray, "Content-Type: application/x-www-form-urlencoded");

我刚刚意识到它不适用于 InternetSetCookie 可能是因为从 Facebook 返回的 Cookie 具有HttpOnly"属性(true),并且无法访问通过客户端脚本.

What I just realized is that it doesn't work with the InternetSetCookie maybe the reason is because Cookies returned from Facebook have the "HttpOnly" attribute (true), and it can't be accessible by client-side script.

这篇关于C# - HttpWebRequest POST(登录到 Facebook)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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