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?
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;
StringBuilder sb = new StringBuilder();
byte[] buf = new byte[8192];
request = (HttpWebRequest)WebRequest.Create(uri);
request.UserAgent = UserAgent;
request.Referer = Refer;
request.CookieContainer = cookies;
try
{
response = (HttpWebResponse)request.GetResponse();
Stream resStream = response.GetResponseStream();
string tempString = null;
int count = 0;
do
{
count = resStream.Read(buf, 0, buf.Length);
if (count != 0)
{
tempString = Encoding.ASCII.GetString(buf, 0, count);
sb.Append(tempString);
}
}
while (count > 0);
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";
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");
}
}
}
}