WebException未处理的错误; [英] WebException Unhandled Error;

查看:92
本文介绍了WebException未处理的错误;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我正在尝试理解为什么我得到一个WebException未处理错误(超时)作为对粗体代码的响应,但是我插入的消息框是获取请求站点的来源。





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
            }
        }
    }
}

推荐答案

您想要显示MessageBox的人是谁?你在这里展示的代码在服务器机器上的IIS服务器内运行...

所以你无法使用像MessageBox这样的可视元素...

你可能想要的从浏览器内的网页向客户端显示该消息...

您的错误表明您并不完全了解客户端和服务器之间的差异以及Web应用程序的分离性质。 ..
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...


不知道原版有什么问题,我终于通过以下课程在我的电脑上运行了代码。



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;
        }
    }
}


这篇关于WebException未处理的错误;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆