- using
- using
- using
- using
- using
- using
- using
- using
- using
- using
- using
- using
- namespace
- {
- public partial class
- {
- new
- string
- public
- {
- InitializeComponent();
- }
- private void Form1_Load( object
- {
- // 读取登录页面,获取 VIEWSTATE 及 Cookies
- byte [] bs = getBytesstring
- "id=/"__VIEWSTATE/" value=/"" ; // viewState 的开始字符串
- int
- int end = html.IndexOf( "/""
- // 获取 viewState
- // 读取图片校验码并显示图片
- , cc, null
- new
- new
- // 显示图片验证码
- ms.Close();
- }
- // 更新图片验证码
- private void pictureBox1_DoubleClick( object
- {
- byte [] bs = getBytes , cc, null
- new
- new
- // 更新图片验证码(可能看不清楚)
- ms.Close();
- }
- // 显示 Cookies 信息
- private void button3_Click( object
- {
- new
- new Uri(foreach (Cookie c in
- {
- " --> "
- }
- richTextBox1.Text = sb.ToString();
- " Bytes"
- }
- // 用户登录
- private void btnLogin_Click( object
- {
- 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=";
- byte [] buffer = Encoding.Default.GetBytes(postData); // 要发送的数据
- string html = Encoding.UTF8.GetString(buffer); // 获取返回的页面内容
- if (html.IndexOf( "您好,您已经成功登录。"
- {
- "你已成功登录 CSDN"
- }
- else
- {
- "登录 CSDN 失败!"
- }
- }
- catch
- {
- MessageBox.Show(Err.Message);
- }
- }
- // 读取网络资源,返回字节数组
- private static byte [] getBytes( string url, CookieContainer cookie, byte
- {
- int c = url.IndexOf( "/"
- byte [] data = null
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
- request.CookieContainer = cookie;
- request.Referer = (c > 0 ? url.Substring(0, c) : url);
- "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"
- "gzip, deflate"
- if (postData != null ) // 需要 Post 数据
- {
- "POST"
- "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
- int ContentLength = ( int
- Stream s = response.GetResponseStream();
- c = 1024 * 10;
- if (ContentLength < 0) // 不能获取数据的长度
- {
- new byte
- new
- int
- while
- {
- ms.Write(data, 0, l);
- l = s.Read(data, 0, c);
- }
- data = ms.ToArray();
- ms.Close();
- }
- else // 数据长度已知
- {
- new byte
- int
- while
- {
- int
- pos += l;
- ContentLength -= l;
- }
- }
- s.Close();
- response.Close();
- if (ce == "gzip" ) // 若数据是压缩格式,则要进行解压
- {
- new MemoryStream(); // 解压后的流
- new MemoryStream(data); // 用于解压的流
- new
- byte [] buffer = new byte [c]; // 读数据缓冲区
- int l = g.Read(buffer, 0, c); // 一次读 10K
- while
- {
- js.Write(buffer, 0, l);
- l = g.Read(buffer, 0, c);
- }
- g.Close();
- ms.Close();
- data = js.ToArray();
- js.Close();
- }
- return data; // 返回字节数组
- }
- }
- }