Issues with loggin and logout functions for bot. 1st login and logout works, second time it fails.

kbhtechkbhtech Abilene, Texas
edited January 2015 in C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;
using System.Web;
using System.Windows.Forms;


namespace Neopets.classes.communications
{
    public delegate void OnCriticalError(object source, CriticalError e);
    public class CriticalError : EventArgs
    {
        private string message;
        public CriticalError(string message)
        {
            this.message = message;
        }
        public string Message()
        {
            return this.message;
        }
    }

    class Authentication
    {
        public event OnCriticalError AuthCriticalError;

        CookieContainer cookies = new CookieContainer();

        private bool is_Loggedin()
        {
            return RequestPage("http://www.neopets.com/index.phtml", "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0", "").Contains("Logout");
        }

        private void AquireCookies(string url)
        {
            HttpWebRequest request = null;
            HttpWebResponse response = null;

            request = (HttpWebRequest)WebRequest.Create(new Uri(url));
            request.Method = "GET";
            request.CookieContainer = cookies;

            try
            {
                response = (HttpWebResponse)request.GetResponse();
            }
            catch (WebException e)
            {
               AuthCriticalError(this, new CriticalError(e.Message));
            }
        }

        private string RequestPage(string uri, string UserAgent, string Refer)
        {

            HttpWebRequest request = null;
            HttpWebResponse response = null;

            // used to build entire input
            StringBuilder sb = new StringBuilder();

            // used on each read operation
            byte[] buf = new byte[8192];

            // prepare the web page we will be asking for
            request = (HttpWebRequest)WebRequest.Create(uri);
            request.UserAgent = UserAgent;
            request.Referer = Refer;
            request.CookieContainer = cookies;

            // execute the request

            try
            {
                response = (HttpWebResponse)request.GetResponse();

                // we will read data via the response stream
                Stream resStream = response.GetResponseStream();

                string tempString = null;
                int count = 0;

                do
                {
                    // fill the buffer with data
                    count = resStream.Read(buf, 0, buf.Length);

                    // make sure we read some data
                    if (count != 0)
                    {
                        // translate from bytes to ASCII text
                        tempString = Encoding.ASCII.GetString(buf, 0, count);

                        // continue building the string
                        sb.Append(tempString);
                    }
                }
                while (count > 0); // any more data to read?

                // print out page source
                return (sb.ToString());
            }
            catch (WebException e)
            {
                AuthCriticalError(this, new CriticalError(e.Message));
                return e.Message;
            }
        }

        private string PostForm(StringBuilder postData, string uri, string UserAgent, string Referer, string ContentType = "application/x-www-form-urlencoded", bool AllowAutoRedirect = true, bool KeepAlive = true)
        {

            HttpWebRequest request = null;
            HttpWebResponse response = null;
            AquireCookies(uri);

            request = (HttpWebRequest)WebRequest.Create(new Uri(uri));
            request.ContentType = ContentType;
            request.UserAgent = UserAgent;
            request.Referer = Referer;
            request.AllowAutoRedirect = AllowAutoRedirect;
            request.KeepAlive = KeepAlive;
            request.CookieContainer = cookies;
            request.Method = "POST";

            //write the POST data to the stream
            try
            {
                using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
                {
                    writer.Write(postData.ToString());
                }

                response = (HttpWebResponse)request.GetResponse();

                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    return reader.ReadToEnd();
                }
            }
            catch(WebException e)
            {
                AuthCriticalError(this, new CriticalError(e.Message));
                return e.Message;
            }

        }

        public int Login(string username, string password)
        {
            if (is_Loggedin() == false)
            {
                StringBuilder PostData = new StringBuilder();
                PostData.Append("destination=%252Findex.phtml");
                PostData.Append("&username=" + HttpUtility.UrlEncode(username));
                PostData.Append("&password=" + HttpUtility.UrlEncode(password));

                string returnData = PostForm(PostData, "http://www.neopets.com/login.phtml", "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0", "http://www.neopets.com", "application/x-www-form-urlencoded", true, true);

                if (returnData.Contains("Incorrect username in cookie."))
                {
                    return 1;
                }

                if (returnData.Contains("Sorry, we did not find an account with that username") || returnData.Contains("No username found! Please go back and re-enter your username.") || returnData.Contains("Sorry, but you have invalid characters in your username."))
                {
                    return 2;
                }

                if (returnData.Contains("Invalid Password. Please enter the correct password to continue."))
                {
                    return 3;
                }

                if (returnData.Contains("Logout"))
                {
                    return 0;
                }

                return 4;
            }
            else
            {
                return 0;
            }
        }

        public void Logout()
        {
            if (is_Loggedin() == true)
            {
                RequestPage("http://www.neopets.com/logout.phtml", "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0", "");
            }
            if (is_Loggedin() == false)
            {
                MessageBox.Show("You Have Logged Out");
            }
        }
    }
}

Comments

  • kbhtechkbhtech Abilene, Texas

    The fail is on the second login, it replies operation has timed out.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion