Click here to Skip to main content
15,884,425 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The first login and logout works well, however once I try to login again it claims that an operation has timed out. can you advice me into troubleshooting and fixing this?


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");
            }
        }
    }
}
Posted
Comments
Tejas Vaishnav 27-Jan-15 5:12am    
Have you try to debug your code, and check on which line of code your program stuck, and taking more time to get result out of it?

1 solution

I've got the original fixed and working on the final class now, the issue was related to the fact that I didn't specify the appropriate headers in the requestpage function.

Thanks Though :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900