我有一些代码可以将我登录到Facebook,只要将您登录后立即返回的页面返回给我,它就能正常工作。 但是我在登录后尝试请求我的实际个人资料而不仅仅是我的新闻Feed。 因此,我必须将我的电子邮件和密码发送到登录页面,然后请求我的个人资料。 如何在请求我的个人资料时保留登录数据?
这是我所拥有的
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;
}
我已经尝试了几种方法来做这个,我已经让它返回我的个人资料,但它返回它,好像我没有登录,你实际上无法查看我的个人资料(因为它是设为私人) 因此,在申请我的个人资料时,我需要以某种方式包含我的登录信息。
答案 0 :(得分:0)
当我制作类似的东西时,这让我感到难过。您需要确保在登录后为您的请求设置了正确的Cookie。尝试将新Cookie添加到CookieCollection
对象,然后再将其应用到您的上一个请求中。
// ^ 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
答案 1 :(得分:0)
您必须在开始时启动CookieCollection并将其用于所有请求。 每当您执行请求时,CookieCollection都会使用新Cookie更新自己,因此使用cookies.Add()也会搞砸它。 但我现在工作得很好。