流程
第一部 正常 方式 访问 需要登录的页面 抓包分析
1 提交的地址 / 分析源码
2 提交的数据构成 --比如含有 别的 网页hash 客户端其他数据
3 取验证码的地址
4 cookies
第二部
1 get 方式 访问 登录 页面
requestWeb = WebRequest.Create("http://localhost/168/index.php?s=Admin-Login") as HttpWebRequest;
requestWeb.Method = "Get";
responseWeb = requestWeb.GetResponse() as HttpWebResponse;
StreamReader dataresponse = new StreamReader(responseWeb.GetResponseStream(), Encoding.UTF8);
string data = dataresponse.ReadToEnd(); //读取返回的流
目的一: 取出提交数据中需要使用的hash值 之类 ..
string data2 = "value=\""; // \" 代表显示" 通过关键字符查找 拿出32位的hash值之类吧
textBox_code.Text = data.Substring(data.IndexOf(data2), 39).Substring(7); //使用indexof 和substring一般可以搞定
//取出 后 保存到文本框
目的二: 拿出建立连接时的cookies保存起来
string cookies = responseWeb.Headers.Get("Set-Cookie"); //获取该连接的cookies
cookies = cookies.Split(';')[0]; //分割出里面的sesionid
textBox_cookies.Text = cookies; //保存到文本框
requestWeb = null;
responseWeb = null;
第三部
利用获取保存的cookies 来 get访问取验证码的地址 并且显示出验证码
http://localhost/168/index.php?s=Admin-Login-Vcode 是取验证码的地址
CookieContainer cc = new CookieContainer();
cc.SetCookies(new Uri("http://localhost/168/index.php?s=Admin-Login-Vcode"), cookies); //将刚才连接保存的cookies 设置加入
requestWeb = WebRequest.Create("http://localhost/168/index.php?s=Admin-Login-Vcode") as HttpWebRequest;
requestWeb.CookieContainer = cc; // 带cookies 进行get访问
requestWeb.Method = "Get";
responseWeb = requestWeb.GetResponse() as HttpWebResponse;
Bitmap img = new Bitmap(responseWeb.GetResponseStream()); //获取验证码图片 保存到pic框中显示
pictureBox1.Image = img;
responseWeb = null;
requestWeb = null;
第四部
还是利用刚才保存的cookies 来进行POST 登陆地址
http://localhost/168/index.php?s=Admin-Login-Check 为post提交地址
CookieContainer cc = new CookieContainer();
cc.SetCookies(new Uri("http://localhost/168/index.php?s=Admin-Login-Check"), textBox_cookies.Text.ToString()); // 将保存在文本框
的cookies 设置到 post地址
string postdata = "user_name=admin&user_pwd=admin&verify=" + textBox_验证码.Text + "&__hash__=" + textBox_code.Text + "&submit.x=25&submit.y=15"; //构建提交的数据 使用到 取得的验证码 输入到验证码框 和 前面获取的hash值
requestWeb = HttpWebRequest.Create("http://localhost/168/index.php?s=Admin-Login-Check") as HttpWebRequest;
requestWeb.Method = "POST";
requestWeb.CookieContainer = cc; // 设置带cookies 来post
requestWeb.ContentType = "application/x-www-form-urlencoded";
StreamWriter datawriter = new StreamWriter(requestWeb.GetRequestStream(), Encoding.Default);
datawriter.Write(postdata); //写入post提交数据
datawriter.Flush();
//----下面为 返回的页面
responseWeb = requestWeb.GetResponse() as HttpWebResponse;
StreamReader datareader = new StreamReader(responseWeb.GetResponseStream(), System.Text.Encoding.UTF8);
textBox_Post返回.Text = datareader.ReadToEnd();