3

编辑:我加了一个赏金,如果有人能帮我弄清楚我做错了什么,这都是你的。

另外,我真的不在乎这是如何完成的。如果有一个图书馆可以提供帮助,或者类似的东西会很棒。

由于不涉及验证码,理论上我应该能够登录 Flickr 并通过类似于以下的代码添加联系人......对吗?

当我运行此代码及其许多变体时,当我到达我应该提交表单以验证我正在添加联系人的地方时,它要求我登录。这可能是我失败的唯一原因收集正确的cookie?

我有 IEHTTPHeader 供 IE 查看实际发生的情况,我正在尽我所能模仿它。除非我只是不明白某些东西,否则我就是找不到哪里出错了。

Flickr API 无法做到这一点

我要做的就是登录并添加一个预定义的联系人。

        //This Kicks off the process
        private void button2_Click(object sender, EventArgs e)
        {
            string user = "adampeditto"; //Some Random Guy
            CookieContainer cookies = new CookieContainer();
            cookies = LoginYahoo(cookies, user);
            cookies = GetCookies(cookies, user);
            cookies = ClickFlickrButton(cookies, user);
        }

        private CookieContainer ClickFlickrButton(CookieContainer cookies, string user)
        {
            try
            {
                string appURL = "http://www.flickr.com/people/" + user + "/relationship";
                string strPostData = "magic_cookie=a9dd9cd9fd23081f35b8bad2c0ba1878&done=1";

                // Setup the http request.
                HttpWebRequest wrWebRequest = WebRequest.Create(appURL) as HttpWebRequest;
                wrWebRequest.Method = "POST";
                wrWebRequest.Timeout = 10000;
                wrWebRequest.ContentLength = strPostData.Length;
                wrWebRequest.ContentType = "application/x-www-form-urlencoded";
                CookieContainer cookieContainer = cookies;
                wrWebRequest.CookieContainer = cookieContainer;

                // Post to the login form.
                StreamWriter swRequestWriter = new
                StreamWriter(wrWebRequest.GetRequestStream());
                swRequestWriter.Write(strPostData);
                swRequestWriter.Close();

                // Get the response.
                HttpWebResponse hwrWebResponse = (HttpWebResponse)wrWebRequest.GetResponse();

                // Read the response
                StreamReader srResponseReader = new StreamReader(hwrWebResponse.GetResponseStream());
                string strResponseData = srResponseReader.ReadToEnd();
                srResponseReader.Close();

                return cookieContainer;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private CookieContainer LoginYahoo(CookieContainer cookies, string user)
        {
            string loginName = "YourLoginName";
            string password = "YourPassword";
            string appURL = "https://login.yahoo.com/config/login?.src=flickr&.pc=5134&.scrumb=0&.pd=c%3DE0.GahOp2e4MjkX.5l2HgAoLkpmyPvccpVM-&.intl=us&.done=https%3A%2F%2Flogin.yahoo.com%2Fconfig%2Fvalidate%3F.src%3Dflickr%26.pc%3D5134%26.scrumb%3D0%26.pd%3Dc%253DE0.GahOp2e4MjkX.5l2HgAoLkpmyPvccpVM-%26.intl%3Dus%26.done%3Dhttp%253A%252F%252Fwww.flickr.com%252Fsignin%252Fyahoo%252F%253Fredir%253D%25252Fpeople%25252F" + user + "%25252Frelationship%25252F&rl=1";
            string strPostData = ".tries=1&.src=flickr&.md5=&.hash=&.js=&.last=&promo=&.intl=us&.bypass=&.partner=&.u=0delt5h5l4df0&.v=0&.challenge=3DZF0DFFqdE0m.9MWnCq6LjUZ9gV&.yplus=&.emailCode=&pkg=&stepid=&.ev=&hasMsgr=1&.chkP=Y&.done=https%3A%2F%2Flogin.yahoo.com%2Fconfig%2Fvalidate%3F.src%3Dflickr%26.pc%3D5134%26.scrumb%3D0%26.pd%3Dc%253DE0.GahOp2e4MjkX.5l2HgAoLkpmyPvccpVM-%26.intl%3Dus%26.done%3Dhttp%253A%252F%252Fwww.flickr.com%252Fsignin%252Fyahoo%252F%253Fredir%253D%25252Fpeople%25252F" + user + "%25252Frelationship%25252F&.pd=flickr_ver%3D0%26c%3DE0.GahOp2e4MjkX.5l2HgAoLkpmyPvccpVM-%26ivt%3D%26sg%3D&login=" + loginName + "&passwd=" + password + "&.persistent=y&.save=Sign+In";

            // Setup the http request.
            HttpWebRequest wrWebRequest = WebRequest.Create(appURL) as

            HttpWebRequest;
            wrWebRequest.AllowAutoRedirect = true;
            wrWebRequest.Method = "POST";
            wrWebRequest.ContentLength = strPostData.Length;
            wrWebRequest.ContentType = "application/x-www-form-urlencoded";
            CookieContainer cookieContainer = cookies;
            wrWebRequest.CookieContainer = cookieContainer;

            // Post to the login form.
            StreamWriter swRequestWriter = new
            StreamWriter(wrWebRequest.GetRequestStream());
            swRequestWriter.Write(strPostData);
            swRequestWriter.Close();

            // Get the response.
            HttpWebResponse hwrWebResponse = (HttpWebResponse)wrWebRequest.GetResponse();

            // Read the response
            StreamReader srResponseReader = new
            StreamReader(hwrWebResponse.GetResponseStream());
            string strResponseData = srResponseReader.ReadToEnd();
            srResponseReader.Close();

            //YOU ARE NOW LOGGED IN TO YAHOO!
            ShowInBrowser(strResponseData);
            return cookieContainer;
        }

        //These are the places it sends you upon autoredirect after your run LoginYahoo(), if you set AutoRedirect to true there should be no need for this...
        private CookieContainer GetCookies(CookieContainer cookies, string user)
        {
            CookieContainer cookieContainer = cookies;

            string appURL = "http://login.yahoo.com/config/validate?.src=flickr&.pc=5134&.scrumb=6l14Ni2Pz3j&.pd=c%3DE0.GahOp2e4MjkX.5l2HgAoLkpmyPvccpVM-&.intl=us&.done=http%3A%2F%2Fwww.flickr.com%2Fsignin%2Fyahoo%2F%3Fredir%3D%252Fpeople%252F" + user + "%252Frelationship%252F";
            HttpWebRequest wrWebRequest = WebRequest.Create(appURL) as HttpWebRequest;
            wrWebRequest.CookieContainer = cookieContainer;
            wrWebRequest.AllowAutoRedirect = false;
            HttpWebResponse hwrWebResponse = (HttpWebResponse)wrWebRequest.GetResponse();

            appURL = "http://flickr.com/signin/yahoo/?redir=%2Fpeople%2F" + user + "%2Frelationship%";
            HttpWebRequest wrWebRequest2 = WebRequest.Create(appURL) as HttpWebRequest;
            wrWebRequest2.CookieContainer = cookieContainer;
            wrWebRequest2.AllowAutoRedirect = false;
            HttpWebResponse hwrWebResponse2 = (HttpWebResponse)wrWebRequest2.GetResponse();

            appURL = "http://flickr.com/cookie_check.gne?pass=%2Fpeople%2F" + user + "%2Frelationship%2F&fail=register_cookies.gne";
            HttpWebRequest wrWebRequest3 = WebRequest.Create(appURL) as HttpWebRequest;
            wrWebRequest3.CookieContainer = cookieContainer;
            wrWebRequest3.AllowAutoRedirect = true;
            HttpWebResponse hwrWebResponse3 = (HttpWebResponse)wrWebRequest3.GetResponse();

            return cookieContainer;
        }
4

2 回答 2

2

我用 perl 的 WWW:Mechanize 库做了类似的事情,看看。

于 2010-01-25T19:00:50.107 回答
2

您可以使用Flickr API,而不是进行屏幕抓取并使您的程序员生活变得艰难。您可能还会发现这篇博文很有帮助。

于 2010-01-20T07:32:40.353 回答