Click here to Skip to main content
15,886,137 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.


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
{
    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 0: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

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...
 
Share this answer
 
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.

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
{
    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;
        }
    }
}
 
Share this answer
 
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