发送POST变量后获取页面源 [英] Getting a page source after POST variables have been sent

查看:63
本文介绍了发送POST变量后获取页面源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have some code that logs me into Facebook and it works fine as far as returning me the page you see right after you log in. But I'm trying to request my actual profile after I log in rather than just my news feed. So I'd have to send my email and password to the login page, then request my profile. How can I keep the login data around while requesting my profile?

这就是我所拥有的

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

我已经尝试了几种方法来获得它返回我的个人资料,但是它会返回它,就像我没有登录一样,并且您实际上无法查看我的个人资料(因为它被设置为私人) 因此,在请求个人资料时,我需要以某种方式包含我的登录信息.

I've tried several ways of doing this and I have gotten it to return my profile, but it returns it as if I was not logged in and you can't actually view my profile (because it is set to private) So I need my login info to be included somehow when requesting my profile.

推荐答案

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

You have to start a CookieCollection at the beginning and use it for all requests. A CookieCollection updates itself with new cookies whenever you do a request so using cookies.Add() was also messing it up. But I got it working fine now.

这篇关于发送POST变量后获取页面源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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