C#类具有登录/注销复杂性。 [英] C# Class has login / logout complications.

查看:65
本文介绍了C#类具有登录/注销复杂性。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首次登录和注销效果很好,但是一旦我尝试再次登录,它就声称操作已经超时。你可以建议我进行故障排除和解决吗?





 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;
使用 System.IO;
使用 System.Net;
使用 System.Web;
使用 System.Windows.Forms;


命名空间 Neopets.classes.communications
{
public delegate void OnCriticalError( object source,CriticalError e);
public class CriticalError:EventArgs
{
private string 消息;
public CriticalError( string message)
{
.message = message;
}
public string 消息()
{
返回 .message;
}
}

class 认证
{
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 )。包含( 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;

// 执行请求

< span class =code-keyword> try

{
response =(HttpWebResponse)request.GetResponse();

// 我们将通过响应流读取数据
Stream resStream = response.GetResponseStream();

string tempString = null ;
int count = 0 ;

执行
{
// 用数据填充缓冲区
count = resStream.Read(buf, 0 ,buf.Length);

// 确保我们读取一些数据
if (count!= 0
{
// 从字节转换为ASCII文本
tempString = Encoding.ASCII.GetString(buf, 0 ,伯爵);

// 继续构建字符串
sb.Append( tempString);
}
}
while (count > 0 ); // 还有更多要阅读的数据吗?

// 打印页面来源
return (sb。的ToString());
}
catch (WebException e)
{
AuthCriticalError( this new CriticalError(e.Message));
return e.Message;
}
}

私人 字符串 PostForm( StringBuilder postData, string uri, string UserAgent, string Referer, string ContentType = application / x- www-form-urlencoded bool AllowAutoRedirect = true bool KeepAlive = true
{

HttpWebRequest request = ;
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;

// 将POST数据写入流
< span class =code-keyword>尝试

{
使用(StreamWriter writer = new StreamWriter(request.GetRequestStream()))
{
writer.Write(postData.ToString());
}

response =(HttpWebResponse)request.GetResponse();

使用(StreamReader reader = new StreamReader(response.GetResponseStream()) )
{
return reader.ReadToEnd();
}
}
catch (WebException e)
{
AuthCriticalError( this new CriticalError(e.Message));
return e.Message;
}

}

public int 登录(字符串用户名,字符串密码)
{
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( cookie中的用户名不正确。))
{
return 1 ;
}

if (returnData.Contains( 抱歉,我们没有找到具有该用户名的帐户)|| returnData.Contains( 找不到用户名!请返回并重新输入您的用户名。)|| returnData.Contains( 抱歉,您的用户名中包含无效字符。))
{
return < span class =code-digit> 2
;
}

if (returnData.Contains( 密码无效。请输入正确的密码才能继续。))
{
return 3 ;
}

if (returnData.Contains( 退出))
{
return 0 ;
}

return 4 ;
}
其他
{
返回 0 ;
}
}

public void 退出( )
{
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( 您已注销);
}
}
}
}

解决方案

我有原版现在修复并处理了最后一课,问题与我没有在请求页面功能中指定相应的标题有关。



谢谢:)

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;

            // 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");
            }
        }
    }
}

解决方案

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 :)


这篇关于C#类具有登录/注销复杂性。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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