Click here to Skip to main content
14,766,265 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Okay, I'm trying to understand why I get a WebException Unhandled Error(Timeout) as a response to the code thats in bold, however the messagebox that I inserted IS Aquiring the source of the requested site.


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
{
    class Authentication
    {
        CookieContainer cookies = new CookieContainer();
        HttpWebRequest request = null;
        HttpWebResponse response = null;
        public int NeopetsLogin(string username, string password)
        {

            // Format Post Strings
            username = HttpUtility.HtmlEncode(username);
            password = HttpUtility.HtmlEncode(password);

            string returnData = string.Empty;

            // WebException

            //Need to retrieve cookies first
            request = (HttpWebRequest)WebRequest.Create(new Uri("http://www.neopets.com/login.phtml"));
            request.Method = "GET";
            request.CookieContainer = cookies;

            response = (HttpWebResponse)request.GetResponse();

            //Set up the request
            request = (HttpWebRequest)WebRequest.Create(new Uri("http://www.neopets.com/login.phtml"));
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0";
            request.Referer = "http://www.google.com/";
            request.AllowAutoRedirect = true;
            request.KeepAlive = true;
            request.CookieContainer = cookies;
            request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";

            //Format the POST data
            StringBuilder postData = new StringBuilder();
            postData.Append("destination=%252Findex.phtml");
            postData.Append("&username=" + username);
            postData.Append("&password=" + password);

            //write the POST data to the stream

            using (StreamWriter writer = new StreamWriter(request.GetRequestStream(), Encoding.ASCII))
                writer.Write(postData.ToString());


            response = (HttpWebResponse)request.GetResponse();

            //Read the web page (HTML) that we retrieve after sending the request

            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                MessageBox.Show(reader.ReadToEnd());
                returnData = reader.ReadToEnd();
            }



            if (returnData.Contains("Logout"))
            {
                return 0; // Login Good
            }
            if (returnData.Contains("Incorrect username in cookie. Please contact support@neopets.com with your browser type and operating system. Thanks!"))
            {
                return 1; // Invalid Username and Password
            }
            if (returnData.Contains("Sorry, we did not find an account with that username."))
            {
                return 2; // Invalid Username and Password
            }
            if (returnData.Contains("Invalid Password. Please enter the correct password to continue."))
            {
                return 3; // Invalid Password
            }
            else
            {
                return 4; // Unkown Error
            }
        }
    }
}
Posted
Updated 14-Jan-15 1:59am
v5
Comments
enhzflep 13-Jan-15 16:42pm
   
Which one is timing-out? You have two of them.
How about the debugger, does that tell you anything useful?
kbhtech 13-Jan-15 17:08pm
   
I've updated to code, and formatted the procedure that is timing out in Bold.
Sergey Alexandrovich Kryukov 13-Jan-15 18:40pm
   
It depends not on just your request, but also on the server-side...
Also, next time, please format code samples properly. I've done it for you, please click "Improve question" and look at the "pre" tag.
—SA
kbhtech 14-Jan-15 7:00am
   
Yeah, I added a messagebox to the function that was triggering the WebException Unhandled Error and suprisingly I was able to view the requested pages source.

I don't think I'm really timing out. so whats happening?
Sergey Alexandrovich Kryukov 14-Jan-15 11:04am
   
If you could see it, the reader wasn't really hanging, otherwise how could it read that page source? Your information is seemingly contradicting...
—SA
kbhtech 14-Jan-15 12:49pm
   
My point, Exactly. Read the header :). I finally that problem though, and running into another problem that has to deal with the login.
Zoltán Zörgő 13-Jan-15 16:44pm
   
Recursive call on error????
Ankur\m/ 14-Jan-15 7:08am
   
I ran your code with dummy username and password (and commenting few lines like show messagebox) and it works fine for me. Put the code in try catch and see the exact error details. If it's timeout, set the timeout to a big value and check again.
kbhtech 14-Jan-15 7:59am
   
I adjusted the timeout to aprox 7 minutes, whenever I ran the code I recieved a timout error in 3-4 minutes. :(
Ankur\m/ 15-Jan-15 1:54am
   
7 minutes is way too much. So is 3-4 minutes. Or did you mean "seconds"?
And did changing timeout worked?
kbhtech 15-Jan-15 10:37am
   
Nope. adjusting the Timeout to 7 minutes didn't work. In fact I recieved a Timout error in approx 4 minutes, even though the Timeout was adjusted to Seven.

I did solve this solution though, I don't know what fixed it but whenever I reloaded the original code, and tweaked it to my needs it seemed to function.

To who you exactly want to show that MessageBox? The code you presented here runs inside the IIS server on the server machine...
So no way you can use a visual element like MessageBox...
Probably you wanted to show that message to the client from the web page inside the browser...
Your error indicates that you are not fully aware of the difference between client and server and of the separated nature of web applications...
   
Comments
kbhtech 14-Jan-15 7:45am
   
Messagebox was an attempt to debug and get a hint of whats going on, I have just now replaced that with a try and catch method.

As far as the class, when its finished there won't be a user interface because it will run inside a service.
Don't know what was wrong with the original, I finally got the code functioning on my PC with the following Class.

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
{
    class Authentication
    {
        CookieContainer cookies = new CookieContainer();
        HttpWebRequest request = null;
        HttpWebResponse response = null;


        public int NeopetsLogin(string username, string password)
        {
            string returnData = string.Empty;

            //Need to retrieve cookies first
            request = (HttpWebRequest)WebRequest.Create(new Uri("http://www.neopets.com/"));
            request.Method = "GET";
            request.CookieContainer = cookies;

            try
            {
                response = (HttpWebResponse)request.GetResponse();
            }
            catch(WebException e)
            {
                if (e.Status == WebExceptionStatus.Timeout)
                {
                    NeopetsLogin(username, password);
                }
                if (e.Status == WebExceptionStatus.ConnectFailure)
                {
                    NeopetsLogin(username, password);
                }
            }

            //Set up the request
            request = (HttpWebRequest)WebRequest.Create(new Uri("http://www.neopets.com/login.phtml"));
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0";
            request.Referer = "http://www.neopets.com/";
            request.AllowAutoRedirect = true;
            request.KeepAlive = true;
            request.CookieContainer = cookies;

            //Format the POST data
            StringBuilder postData = new StringBuilder();
            postData.Append("destination=%252Findex.phtml");
            postData.Append("&username=kbhtech");
            postData.Append("&password=***PASWORDREMOVEDFORSECURITYPURPOSES***");


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


            try
            {
                response = (HttpWebResponse)request.GetResponse();
            }
            catch (WebException e)
            {
                if (e.Status == WebExceptionStatus.Timeout)
                {
                    NeopetsLogin(username, password);
                }
                if (e.Status == WebExceptionStatus.ConnectFailure)
                {
                    NeopetsLogin(username, password);
                }
            }
            

            //Read the web page (HTML) that we retrieve after sending the request
            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                returnData = reader.ReadToEnd();

            if (returnData.Contains("Error: Incorrect username in cookie. Please contact support@neopets.com with your browser type and operating system. Thanks!"))
            {
                return 1;
            }


            if (returnData.Contains("Sorry, we did not find an account with that 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;
        }
    }
}
   
v2

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