C# - HttpWebRequest POST(登录 Facebook)

标签 c# facebook post authentication httpwebrequest

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

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

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

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

编辑: 我在发布后不久就知道了如何操作。

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

还是谢谢! :)

最佳答案

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

第一个请求:获取 cookie。

 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。

 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,也可以使用以下方法:

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

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

关于C# - HttpWebRequest POST(登录 Facebook),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8425593/

相关文章:

php - 从数据库中删除用户 - GET 方法更改为 POST 方法

c# - 为什么不能在 C# 中将 Powershell 作为常规进程调用

java - 为什么 Facebook 登录无法在 Android 中的签名版本 APK 中工作?

javascript - 通过 fb 登录网站时,我的函数 Test Api 不收集用户数据

javascript - 存储附加到用户贡献内容的 YouTube 内容的最佳方法是什么?

http - 在 Arduino 上接收 HTTP POST 请求

Android:连接超时不起作用(连接永远等待)

c# - 通过套接字发送数据时计算字节长度时出现问题

c# - Caliburn Micro Guard Methods 不评估属性变化

c# - 提取短语中的双引号项