예제 #1
0
        public bool doLogin(string username, string password)
        {
            this.username = username;
            this.password = password;

            if (!Directory.Exists(@"sessions\" + username))
            {
                DirectoryInfo di = Directory.CreateDirectory(@"sessions\" + username);
            }

            File.Delete(@"sessions\" + username + @"\httpSessionLog.txt");
            this.http.SessionLogFilename = @"sessions\" + username + @"\httpSessionLog.txt";
            this.http.CookieDir = @"sessions\" + username;
            this.http.SetCookieXml(this.twitterHost, this.http.GetCookieXml(this.twitterHost) );
            this.http.SendCookies = true;
            this.http.SaveCookies = true;

            if (!this.getAuthenticityToken()) return false;

            if (this.is_loggedin) return true;

            Console.WriteLine("Logging in.....");

            //  Build an HTTP POST request to login
            this.req.RemoveAllParams();
            this.req.UsePost();
            this.req.Path = this.twitterLoginPath;

            //add params
            this.req.AddParam("session[username_or_email]", username);
            this.req.AddParam("session[password]", password);
            this.req.AddParam("authenticity_token", this.authenticity_token);
            this.req.AddParam("scribe_log", "");
            this.req.AddParam("redirect_after_login", "/" + username);

            this.req.AddHeader("Referer", this.twitterMainUrl);

            //  Send the HTTP POST and get the response.  Note: This is a blocking call.
            //  The method does not return until the full HTTP response is received.
            this.resp = this.http.SynchronousRequest(this.twitterHost, this.port, this.use_ssl, this.req);
            if (this.resp == null)
            {
                Console.WriteLine("ERROR: " + this.http.LastErrorText + "\n");
                return false;
            }

            //  Is this a 302 redirect?
            if (this.resp.StatusCode == 302 || this.resp.StatusCode == 200)
            {
                //  Get the redirect URL:
                string redirectUrl = resp.GetHeaderField("location");

                string html = String.Empty;

                //Console.WriteLine(resp.Header);
                //Console.WriteLine("Location: " + redirectUrl);

                if (redirectUrl != null && redirectUrl != String.Empty)
                {
                    if ( redirectUrl.Contains("https://twitter.com/login/error") ||
                         redirectUrl.Contains("https://twitter.com/login/captcha")
                        )
                    {
                        return false;
                    }
                    html = http.QuickGetStr(redirectUrl);
                }
                else
                {
                    html = this.resp.BodyStr;
                }

                //this.getAuthenticityToken(html);
            }
            else
            {
                //Console.WriteLine("ERROR: " + this.resp.BodyStr + "\n");
                Console.WriteLine("ERROR: " + this.resp.StatusCode);
                return false;
            }

            this.is_loggedin = true;
            return true;
        }