ASP.NET学习社区ASP.NET学习区有问有答 【请教】asp.net模拟登陆为何不成功

1  /  1  页   1 跳转 查看:225

【请教】asp.net模拟登陆为何不成功

【请教】asp.net模拟登陆为何不成功



using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Net;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
namespace hibaiduTest
{
    /// <summary>
    /// Summary description for WebForm1.
    /// </summary>
    public partial class getHttpInfo : System.Web.UI.Page
    {
        protected static string cookieheader;

        private void Page_Load(object sender, System.EventArgs e)
        {
            // Put user code to initialize the page here
            string strResult;
            if (HttpContext.Current.Application["cookieheader"] != null)
            {
                cookieheader = (string)HttpContext.Current.Application["cookieheader"];
            }
            else
            {
                //Login into the website and keep the cookie for the session in the application variable
                string strLogin = Login("http://passport.baidu.com/?login", "act=login&username=testuser2008&password=testuser2008");
            }

            strResult = getPage("http://passport.baidu.com/?login", "act=login&username=testuser2008&password=testuser2008");

            //Write the result to htm file
            FileStream htmFile = new FileStream(Server.MapPath("save.txt"), FileMode.OpenOrCreate);
            StreamWriter sw = new StreamWriter(htmFile);
            sw.Write(strResult);
            sw.Close();
            htmFile.Close();
            // output the result
            //Response.Write(strResult);
            //Response.Write(Server.MapPath("save.txt"));
        }

        public static string Login(String url, String paramList)
        {
            HttpWebResponse res = null;
            string strResult = "";
            try
            {
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                req.Method = "POST";
                req.ContentType = "application/x-www-form-urlencoded";
                req.AllowAutoRedirect = false;
                CookieContainer cookieCon = new CookieContainer();
                req.CookieContainer = cookieCon;
                StringBuilder UrlEncoded = new StringBuilder();
                Char[] reserved = { '?', '=', '&' };
                byte[] SomeBytes = null;
                if (paramList != null)
                {
                    int i = 0, j;
                    while (i < paramList.Length)
                    {
                        j = paramList.IndexOfAny(reserved, i);
                        if (j == -1)
                        {
                            UrlEncoded.Append(HttpUtility.UrlEncode(paramList.Substring(i, paramList.Length - i)));
                            break;
                        }
                        UrlEncoded.Append(HttpUtility.UrlEncode(paramList.Substring(i, j - i)));
                        UrlEncoded.Append(paramList.Substring(j, 1));
                        i = j + 1;
                    }
                    SomeBytes = Encoding.UTF8.GetBytes(UrlEncoded.ToString());
                    req.ContentLength = SomeBytes.Length;
                    Stream newStream = req.GetRequestStream();
                    newStream.Write(SomeBytes, 0, SomeBytes.Length);
                    newStream.Close();
                }
                else
                {
                    req.ContentLength = 0;
                }

                res = (HttpWebResponse)req.GetResponse();
                cookieheader = req.CookieContainer.GetCookieHeader(new Uri(url));
                HttpContext.Current.Application.Lock();
                HttpContext.Current.Application["cookieheader"] = cookieheader;
                HttpContext.Current.Application.UnLock();
                Stream ReceiveStream = res.GetResponseStream();
                Encoding encode = System.Text.Encoding.GetEncoding("gb2312");
                StreamReader sr = new StreamReader(ReceiveStream, encode);
                Char[] read = new Char[256];
                int count = sr.Read(read, 0, 256);
                while (count > 0)
                {
                    String str = new String(read, 0, count);
                    strResult += str;
                    count = sr.Read(read, 0, 256);
                }
            }
            catch (Exception e)
            {
                strResult = e.ToString();
            }
            finally
            {
                if (res != null)
                {
                    res.Close();
                }
            }
            return strResult;
        }

        public static string getPage(String url, String paramList)
        {
            HttpWebResponse res = null;
            string strResult = "";
            try
            {
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                req.Method = "POST";
                req.KeepAlive = true;
                req.ContentType = "application/x-www-form-urlencoded";
                CookieContainer cookieCon = new CookieContainer();
                req.CookieContainer = cookieCon;
                req.CookieContainer.SetCookies(new Uri(url), cookieheader);
                StringBuilder UrlEncoded = new StringBuilder();
                Char[] reserved = { '?', '=', '&' };
                byte[] SomeBytes = null;
                if (paramList != null)
                {
                    int i = 0, j;
                    while (i < paramList.Length)
                    {
                        j = paramList.IndexOfAny(reserved, i);
                        if (j == -1)
                        {
                            UrlEncoded.Append(HttpUtility.UrlEncode(paramList.Substring(i, paramList.Length - i)));
                            break;
                        }
                        UrlEncoded.Append(HttpUtility.UrlEncode(paramList.Substring(i, j - i)));
                        UrlEncoded.Append(paramList.Substring(j, 1));
                        i = j + 1;
                    }
                    SomeBytes = Encoding.UTF8.GetBytes(UrlEncoded.ToString());
                    req.ContentLength = SomeBytes.Length;
                    Stream newStream = req.GetRequestStream();
                    newStream.Write(SomeBytes, 0, SomeBytes.Length);
                    newStream.Close();
                }
                else
                {
                    req.ContentLength = 0;
                }

                res = (HttpWebResponse)req.GetResponse();
                Stream ReceiveStream = res.GetResponseStream();
                Encoding encode = System.Text.Encoding.GetEncoding("gb2312");
                StreamReader sr = new StreamReader(ReceiveStream, encode);
                Char[] read = new Char[256];
                int count = sr.Read(read, 0, 256);
                while (count > 0)
                {
                    String str = new String(read, 0, count);
                    strResult += str;
                    count = sr.Read(read, 0, 256);
                }
            }
            catch (Exception e)
            {
                strResult = e.ToString();
            }
            finally
            {
                if (res != null)
                {
                    res.Close();
                }
            }
            return strResult;
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);
        }
        #endregion

    }
}


 

回复:【请教】asp.net模拟登陆为何不成功

以后问题帖 请直接发布在本栏目。下载区不允许提问

百度登陆过程做了两三次跳转(好象有服务器端跳转也有客户端js脚本跳转),所以你直接捕获当然是不行地。要想真正登陆,估计要好好分析一下
 
1  /  1  页   1 跳转

版权所有 ASP.NET学习门户 2.0.1214   Sitemap  

返顶部