模拟CSDN登录

  1. using  System;  
  2. using  System.Collections.Generic;  
  3. using  System.ComponentModel;  
  4. using  System.Data;  
  5. using  System.Drawing;  
  6. using  System.Linq;  
  7. using  System.Text;  
  8. using  System.Windows.Forms;  
  9.   
  10. using  System.Net;  
  11. using  System.IO;  
  12. using  System.IO.Compression;  
  13. using  System.Web;  
  14.   
  15. namespace  loginCSDN  
  16. {  
  17.     public  partial  class  Form1 : Form  
  18.     {  
  19.         CookieContainer cc = new  CookieContainer();  
  20.         string  viewState;  
  21.   
  22.         public  Form1()  
  23.         {  
  24.             InitializeComponent();  
  25.         }  
  26.   
  27.         private   void  Form1_Load( object  sender, EventArgs e)  
  28.         {  
  29.             // 读取登录页面,获取 VIEWSTATE 及 Cookies   
  30.             byte [] bs = getBytes( "http://passport.csdn.net/UserLogin.aspx" , cc,  null );  
  31.             string  html = Encoding.UTF8.GetString(bs);  
  32.             viewState = "id=/"__VIEWSTATE/" value=/"" ;                       // viewState 的开始字符串   
  33.             int  begin = html.IndexOf(viewState) + viewState.Length;  
  34.             int  end = html.IndexOf( "/"" , begin);  
  35.             viewState = html.Substring(begin, end - begin);                 // 获取 viewState   
  36.   
  37.             // 读取图片校验码并显示图片   
  38.             bs = getBytes("http://passport.csdn.net/ShowExPwd.aspx?temp=ga1166l0" , cc,  null );  
  39.             MemoryStream ms = new  MemoryStream(bs);  
  40.             Bitmap bmp = new  Bitmap(ms);  
  41.             pictureBox1.Image = bmp;                                        // 显示图片验证码   
  42.             ms.Close();  
  43.         }  
  44.   
  45.         // 更新图片验证码   
  46.         private   void  pictureBox1_DoubleClick( object  sender, EventArgs e)  
  47.         {  
  48.             byte [] bs = getBytes( "http://passport.csdn.net/ShowExPwd.aspx?temp=ga1166l0" , cc,  null );  
  49.             MemoryStream ms = new  MemoryStream(bs);  
  50.             Bitmap bmp = new  Bitmap(ms);  
  51.             pictureBox1.Image = bmp;                                        // 更新图片验证码(可能看不清楚)   
  52.             ms.Close();  
  53.         }  
  54.   
  55.         // 显示 Cookies 信息   
  56.         private   void  button3_Click( object  sender, EventArgs e)  
  57.         {  
  58.             StringBuilder sb = new  StringBuilder();  
  59.             CookieCollection cCollection = cc.GetCookies(new  Uri( "http://passport.csdn.net/UserLogin.aspx" ));  
  60.             foreach  (Cookie c  in  cCollection)  
  61.             {  
  62.                 sb.AppendLine(c.Name.PadLeft(20) + " --> "  + c.Value);  
  63.             }  
  64.             richTextBox1.Text = sb.ToString();  
  65.             label1.Text = richTextBox1.Text.Length.ToString() + " Bytes" ;  
  66.         }  
  67.   
  68.         // 用户登录   
  69.         private   void  btnLogin_Click( object  sender, EventArgs e)  
  70.         {  
  71.             try   
  72.             {  
  73.                 string  userName = txtUser.Text;                              // 获取用户名   
  74.                 string  userPwd = txtPassword.Text;                           // 获取口令   
  75.                 string  Verify = txtVerify.Text;                              // 获取验证码   
  76.                 string  postData =  "__VIEWSTATE="  + HttpUtility.UrlEncode(viewState) +  "&ctl00$CPH_Content$tb_LoginNameOrLoginEmail="  + userName +  "&ctl00$CPH_Content$tb_Password="  + userPwd +  "&ctl00$CPH_Content$tb_ExPwd="  + Verify +  "&ctl00$CPH_Content$Image_Login.x=16&ctl00$CPH_Content$Image_Login.y=17" ;   //&from=http://hi.csdn.net/&PrePage=&MailParameters=";   
  77.                 byte [] buffer = Encoding.Default.GetBytes(postData);         // 要发送的数据   
  78.                 buffer = getBytes("http://passport.csdn.net/UserLogin.aspx" , cc, buffer);  
  79.                 string  html = Encoding.UTF8.GetString(buffer);               // 获取返回的页面内容   
  80.   
  81.                 if  (html.IndexOf( "您好,您已经成功登录。" ) > 0)  
  82.                 {  
  83.                     MessageBox.Show("你已成功登录 CSDN" );  
  84.                 }  
  85.                 else   
  86.                 {  
  87.                     MessageBox.Show("登录 CSDN 失败!" );  
  88.                 }  
  89.             }  
  90.             catch  (Exception Err)  
  91.             {  
  92.                 MessageBox.Show(Err.Message);  
  93.             }  
  94.         }  
  95.   
  96.         // 读取网络资源,返回字节数组   
  97.         private   static   byte [] getBytes( string  url, CookieContainer cookie,  byte [] postData)  
  98.         {  
  99.             int  c = url.IndexOf( "/" , 10);  
  100.             byte [] data =  null ;  
  101.             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);  
  102.             request.CookieContainer = cookie;  
  103.             request.Referer = (c > 0 ? url.Substring(0, c) : url);  
  104.             request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)" ;  
  105.             request.Headers[HttpRequestHeader.AcceptEncoding] = "gzip, deflate" ;  
  106.   
  107.             if  (postData !=  null )                                            // 需要 Post 数据   
  108.             {  
  109.                 request.Method = "POST" ;  
  110.                 request.ContentType = "application/x-www-form-urlencoded" ;  
  111.                 request.ContentLength = postData.Length;  
  112.                 Stream requestStream = request.GetRequestStream();  
  113.                 requestStream.Write(postData, 0, postData.Length);  
  114.                 requestStream.Close();  
  115.             }  
  116.   
  117.             HttpWebResponse response = (HttpWebResponse)request.GetResponse();  
  118.             string  ce = response.Headers[HttpResponseHeader.ContentEncoding];  
  119.             int  ContentLength = ( int )response.ContentLength;  
  120.             Stream s = response.GetResponseStream();  
  121.             c = 1024 * 10;  
  122.             if  (ContentLength < 0)                                           // 不能获取数据的长度   
  123.             {  
  124.                 data = new   byte [c];  
  125.                 MemoryStream ms = new  MemoryStream();  
  126.                 int  l = s.Read(data, 0, c);  
  127.                 while  (l > 0)  
  128.                 {  
  129.                     ms.Write(data, 0, l);  
  130.                     l = s.Read(data, 0, c);  
  131.                 }  
  132.                 data = ms.ToArray();  
  133.                 ms.Close();  
  134.             }  
  135.             else                                                              // 数据长度已知   
  136.             {  
  137.                 data = new   byte [ContentLength];  
  138.                 int  pos = 0;  
  139.                 while  (ContentLength > 0)  
  140.                 {  
  141.                     int  l = s.Read(data, pos, ContentLength);  
  142.                     pos += l;  
  143.                     ContentLength -= l;  
  144.                 }  
  145.             }  
  146.             s.Close();  
  147.             response.Close();  
  148.   
  149.             if  (ce ==  "gzip" )                                                // 若数据是压缩格式,则要进行解压   
  150.             {  
  151.                 MemoryStream js = new  MemoryStream();                        // 解压后的流      
  152.                 MemoryStream ms = new  MemoryStream(data);                    // 用于解压的流      
  153.                 GZipStream g = new  GZipStream(ms, CompressionMode.Decompress);  
  154.                 byte [] buffer =  new   byte [c];                                 // 读数据缓冲区         
  155.                 int  l = g.Read(buffer, 0, c);                                // 一次读 10K         
  156.                 while  (l > 0)  
  157.                 {  
  158.                     js.Write(buffer, 0, l);  
  159.                     l = g.Read(buffer, 0, c);  
  160.                 }  
  161.                 g.Close();  
  162.                 ms.Close();  
  163.                 data = js.ToArray();  
  164.                 js.Close();  
  165.             }  
  166.             return  data;                                                     // 返回字节数组   
  167.         }  
  168.     }  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Java海洋

你的鼓励,是我写下去的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值