net 网站自动登录

楼主,根据你的意思,我自己用代码写了一个CSDN的登录器,发给你参考一下,下面说说具体的思路.

首先,我们先定义一些变量:
C# code

        private const string NET_SESSIONID= "ASP.NET_SessionId=";
        private const string CLIENTKEY = "ClientKey=";

        string aspcookie = "";
        private string html = "";
        private string sessionId = "";
        private string clientKey = "";
        private string viewState = "";

一、利用HttpWebRequest获取CSDN的登录信息,如ASP.NET_SessionId、ClientKey、__VIEWSTATE。这步可以放在Login_Load中,下面是代码:
C# code

        private void Login_Load(object sender, EventArgs e)
        {
            HttpWebRequest request = WebRequest.Create("http://passport.csdn.net/UserLogin.aspx") as HttpWebRequest;
            request.Credentials = CredentialCache.DefaultCredentials;
            request.Accept = "*/*";
            request.Referer = "http://passport.csdn.net/UserLogin.aspx";
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E)";
            request.Method = "GET";
            request.KeepAlive = true;
            request.Headers.Set("Accept-Language", "zh-CN");
            request.Headers.Set("Accept-Encoding", "gzip, deflate");

            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            System.IO.Stream responseStream = response.GetResponseStream();
            System.IO.StreamReader reader = new System.IO.StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
            html = reader.ReadToEnd();

            string viewStateFlag = "id=\"__VIEWSTATE\" value=\"";
            int i = html.IndexOf(viewStateFlag) + viewStateFlag.Length;
            int j = html.IndexOf("\"", i);
            viewState = html.Substring(i, j - i);

            aspcookie = response.Headers.Get("Set-Cookie");
            sessionId = GetSessionId();
            clientKey = GetClientKey();
            // 获取图片验证码
            GetCode();
        }
         /// <summary>
        /// 获取ClientKey
        /// </summary>
        /// <returns></returns>
        private string GetClientKey()
        {
            // 这个仅仅是用来获取ClientKey值的长度
            string id = "bb32434c-3bb3-4e44-92c3-8952f631ca87";
            int index = aspcookie.IndexOf(CLIENTKEY) + CLIENTKEY.Length;
            String str = aspcookie.Substring(index, id.Length);
            return str;
        }

        /// <summary>
        /// 获取ASP.NET_SessionId=
        /// </summary>
        /// <returns></returns>
        private string GetSessionId()
        {
            string id = "5mhl0tvbw5shlpnhxgwnck45";
            int index = aspcookie.IndexOf(NET_SESSIONID) + NET_SESSIONID.Length;
            String str = aspcookie.Substring(index, id.Length);
            return str;
        }

     

二、获取图片验证码,在这一步中需要注意的是,因为我们用的是另外一个HttpWebRequest去获取,所以为了保持获取的验证码与登录页面的Session一致,我们需要把Session等信息放入Header中去,下面是代码:
C# code

/// <summary>
        /// 产生新的验证码
        /// </summary>
        private void GetCode()
        {
            try
            {
                CookieCollection cookies = new CookieCollection();
                cookies.Add(new Cookie("ASP.NET_SessionId",sessionId));
                cookies.Add(new Cookie("ClientKey",clientKey));

                HttpWebRequest httpWebRequest;
                HttpWebResponse webResponse;
                byte[] byteRequest = { };
                httpWebRequest = (HttpWebRequest)HttpWebRequest.Create("http://passport.csdn.net/ShowExPwd.aspx");
                CookieContainer co = new CookieContainer();
                co.Add(new Uri("http://passport.csdn.net"), cookies);
                httpWebRequest.CookieContainer = co;
                httpWebRequest.Accept = "*/*";
                httpWebRequest.Referer = "http://passport.csdn.net/UserLogin.aspx";
                httpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E)";
                httpWebRequest.Method = "GET";
                httpWebRequest.Headers.Set("Accept-Encoding", "gzip, deflate");
                httpWebRequest.Headers.Set("Accept-Language", "zh-CN");
                httpWebRequest.KeepAlive = true;

                webResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                Stream responseStream = webResponse.GetResponseStream();
                Image original = Image.FromStream(responseStream);
                Bitmap bitMap = new Bitmap(original);
                this.pictureBox1.Image = bitMap;
                responseStream.Close();
            }
            catch (Exception exception)
            {
                MessageBox.Show("ERROR:" + exception.Message);
            }
        }

三、最后一步是登录事件,登录很简单,把相关的信息放入请求数据中就行了。
C# code

private void btnLogin_Click(object sender, EventArgs e)
        {
            HttpWebRequest httpWebRequest;
            HttpWebResponse webResponse;

            string randnum = txtRandnum.Text;
            string password = txtPassword.Text;
            string loginname = txtUserName.Text;

            string postData = "ctl00$CPH_Content$tb_LoginNameOrLoginEmail={0}&ctl00$CPH_Content$tb_Password={1}&ctl00$CPH_Content$tb_ExPwd={2}&__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE={3}&ClientKey={4}&from={5}&MailParameters=&PrePage=&ctl00$CPH_Content$Image_Login.x=46&ctl00$CPH_Content$Image_Login.y=9";
            postData = string.Format(postData, System.Web.HttpUtility.UrlEncode(loginname),System.Web.HttpUtility.UrlEncode(password),System.Web.HttpUtility.UrlEncode(randnum), System.Web.HttpUtility.UrlEncode(viewState), clientKey, System.Web.HttpUtility.UrlEncode("http://hi.csdn.net/"));
            byte[] byteRequest = Encoding.UTF8.GetBytes(postData);

            CookieCollection cookies = new CookieCollection();
            cookies.Add(new Cookie("ASP.NET_SessionId", sessionId));
            cookies.Add(new Cookie("ClientKey", clientKey));
            cookies.Add(new Cookie("UN",loginname));

            httpWebRequest = (HttpWebRequest)HttpWebRequest.Create("http://passport.csdn.net/UserLogin.aspx");
            CookieContainer co = new CookieContainer();
            co.Add(new Uri("http://passport.csdn.net"), cookies);
            httpWebRequest.CookieContainer = co;
            httpWebRequest.ContentType = "application/x-www-form-urlencoded";
            httpWebRequest.Accept = "image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/x-shockwave-flash, */*";
            httpWebRequest.Referer = "http://passport.csdn.net/UserLogin.aspx";
            httpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E)";
            httpWebRequest.Method = "POST";
            httpWebRequest.KeepAlive = true;
            httpWebRequest.Headers.Set("Cache-Control", "no-cache");
            httpWebRequest.Headers.Set("Accept-Encoding", "gzip, deflate");
            httpWebRequest.Headers.Set("Accept-Language", "zh-CN");

            httpWebRequest.ContentLength = byteRequest.Length;
            Stream stream;
            stream = httpWebRequest.GetRequestStream();
            stream.Write(byteRequest, 0, byteRequest.Length);
            stream.Close();
            webResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            System.IO.Stream responseStream = webResponse.GetResponseStream();

            String header = webResponse.Headers.ToString();
            Stream getStream = webResponse.GetResponseStream();
            long contentLength = webResponse.ContentLength;

            byte[] outBytes = new byte[contentLength];
            outBytes = ReadFully(getStream);
            getStream.Close();

            getStream = new MemoryStream(outBytes);
            StreamReader streamReader = new StreamReader(getStream, Encoding.UTF8);
            string getString = streamReader.ReadToEnd();
            streamReader.Close();
            getStream.Close();

        }

        static byte[] ReadFully(Stream stream)
        {
            byte[] buffer = new byte[128];
            using (MemoryStream ms = new MemoryStream())
            {
                while (true)
                {
                    int read = stream.Read(buffer, 0, buffer.Length);
                    if (read <= 0)
                        return ms.ToArray();
                    ms.Write(buffer, 0, read);
                }
            }
        }

其中,在如果把数据以及需要把哪些数据Post到服务器中花费了不少时间,后来利用HttpAnalyzer来抓取浏览器中登录的数据包与WinForm登录的数据包进行了对比,才找出问题所在。

posted on 2010-07-29 10:33  风乔  阅读(260)  评论(0编辑  收藏  举报

导航