- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Net;
- using System.IO;
- using System.IO.Compression;
- using System.Web;
- namespace loginCSDN
- {
- public partial class Form1 : Form
- {
- CookieContainer cc = new CookieContainer();
- string viewState;
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load( object sender, EventArgs e)
- {
- // 读取登录页面,获取 VIEWSTATE 及 Cookies
- byte [] bs = getBytes( "http://passport.csdn.net/UserLogin.aspx" , cc, null );
- string html = Encoding.UTF8.GetString(bs);
- viewState = "id=/"__VIEWSTATE/" value=/"" ; // viewState 的开始字符串
- int begin = html.IndexOf(viewState) + viewState.Length;
- int end = html.IndexOf( "/"" , begin);
- viewState = html.Substring(begin, end - begin); // 获取 viewState
- // 读取图片校验码并显示图片
- bs = getBytes("http://passport.csdn.net/ShowExPwd.aspx?temp=ga1166l0" , cc, null );
- MemoryStream ms = new MemoryStream(bs);
- Bitmap bmp = new Bitmap(ms);
- pictureBox1.Image = bmp; // 显示图片验证码
- ms.Close();
- }
- // 更新图片验证码
- private void pictureBox1_DoubleClick( object sender, EventArgs e)
- {
- byte [] bs = getBytes( "http://passport.csdn.net/ShowExPwd.aspx?temp=ga1166l0" , cc, null );
- MemoryStream ms = new MemoryStream(bs);
- Bitmap bmp = new Bitmap(ms);
- pictureBox1.Image = bmp; // 更新图片验证码(可能看不清楚)
- ms.Close();
- }
- // 显示 Cookies 信息
- private void button3_Click( object sender, EventArgs e)
- {
- StringBuilder sb = new StringBuilder();
- CookieCollection cCollection = cc.GetCookies(new Uri( "http://passport.csdn.net/UserLogin.aspx" ));
- foreach (Cookie c in cCollection)
- {
- sb.AppendLine(c.Name.PadLeft(20) + " --> " + c.Value);
- }
- richTextBox1.Text = sb.ToString();
- label1.Text = richTextBox1.Text.Length.ToString() + " Bytes" ;
- }
- // 用户登录
- private void btnLogin_Click( object sender, EventArgs e)
- {
- try
- {
- string userName = txtUser.Text; // 获取用户名
- string userPwd = txtPassword.Text; // 获取口令
- string Verify = txtVerify.Text; // 获取验证码
- 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=";
- byte [] buffer = Encoding.Default.GetBytes(postData); // 要发送的数据
- buffer = getBytes("http://passport.csdn.net/UserLogin.aspx" , cc, buffer);
- string html = Encoding.UTF8.GetString(buffer); // 获取返回的页面内容
- if (html.IndexOf( "您好,您已经成功登录。" ) > 0)
- {
- MessageBox.Show("你已成功登录 CSDN" );
- }
- else
- {
- MessageBox.Show("登录 CSDN 失败!" );
- }
- }
- catch (Exception Err)
- {
- MessageBox.Show(Err.Message);
- }
- }
- // 读取网络资源,返回字节数组
- private static byte [] getBytes( string url, CookieContainer cookie, byte [] postData)
- {
- int c = url.IndexOf( "/" , 10);
- byte [] data = null ;
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
- request.CookieContainer = cookie;
- request.Referer = (c > 0 ? url.Substring(0, c) : url);
- request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)" ;
- request.Headers[HttpRequestHeader.AcceptEncoding] = "gzip, deflate" ;
- if (postData != null ) // 需要 Post 数据
- {
- request.Method = "POST" ;
- request.ContentType = "application/x-www-form-urlencoded" ;
- request.ContentLength = postData.Length;
- Stream requestStream = request.GetRequestStream();
- requestStream.Write(postData, 0, postData.Length);
- requestStream.Close();
- }
- HttpWebResponse response = (HttpWebResponse)request.GetResponse();
- string ce = response.Headers[HttpResponseHeader.ContentEncoding];
- int ContentLength = ( int )response.ContentLength;
- Stream s = response.GetResponseStream();
- c = 1024 * 10;
- if (ContentLength < 0) // 不能获取数据的长度
- {
- data = new byte [c];
- MemoryStream ms = new MemoryStream();
- int l = s.Read(data, 0, c);
- while (l > 0)
- {
- ms.Write(data, 0, l);
- l = s.Read(data, 0, c);
- }
- data = ms.ToArray();
- ms.Close();
- }
- else // 数据长度已知
- {
- data = new byte [ContentLength];
- int pos = 0;
- while (ContentLength > 0)
- {
- int l = s.Read(data, pos, ContentLength);
- pos += l;
- ContentLength -= l;
- }
- }
- s.Close();
- response.Close();
- if (ce == "gzip" ) // 若数据是压缩格式,则要进行解压
- {
- MemoryStream js = new MemoryStream(); // 解压后的流
- MemoryStream ms = new MemoryStream(data); // 用于解压的流
- GZipStream g = new GZipStream(ms, CompressionMode.Decompress);
- byte [] buffer = new byte [c]; // 读数据缓冲区
- int l = g.Read(buffer, 0, c); // 一次读 10K
- while (l > 0)
- {
- js.Write(buffer, 0, l);
- l = g.Read(buffer, 0, c);
- }
- g.Close();
- ms.Close();
- data = js.ToArray();
- js.Close();
- }
- return data; // 返回字节数组
- }
- }
- }
模拟CSDN登录
最新推荐文章于 2021-02-04 06:10:25 发布