WebRequest 模拟请求登录 终于搞定了!

由于自己的大意,看了别人的错误例子,却没好好反省。

 

其实有些网上传的代码是不对的,最起码针对我要登录的这个网站是不可行的。

本以为声明个cookiecontainer ,只有request设置了cookicontainer,那么请求就会自动加载cookiecontainer中的cookier到header中。那是错误滴,他的作用是 response 返回的header的cookie 可以获取的到,如果不设置cookiecontaienr,就获取不到返回的cookie。

 

 

下面来上代码吧,由于代码很杂乱。过几天自己来重新整理下。

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.Threading; namespace maidongxi { public partial class Form1 : Form { HttpWebRequest request = null; HttpWebResponse response = null; string cookiestr = null; CookieContainer cc = new CookieContainer(); string local = null; public static ManualResetEvent alldone=new ManualResetEvent(false); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { webBrowser1.Navigate("http://ucenter.maidongxi.com/"); } private void button1_Click(object sender, EventArgs e) { webBrowser1.Navigate("http://ucenter.maidongxi.com/"); } private void button2_Click(object sender, EventArgs e) { //cookiestr = webBrowser1.Document.Cookie.ToString(); cookiestr = ""; MessageBox.Show(cookiestr); //带上cookie,获取验证码 request = (HttpWebRequest)WebRequest.Create("http://ucenter.maidongxi.com/ValidateImage.aspx?id=216"); request.CookieContainer = cc; request.Headers.Add("Cookie:"+cookiestr); response = (HttpWebResponse)request.GetResponse(); System.Drawing.Image img; img = new System.Drawing.Bitmap(response.GetResponseStream()); pictureBox1.Image = img; response.Close(); // } private void button3_Click(object sender, EventArgs e) { //登录 string postdata = "__EVENTTARGET=loginnner&__EVENTARGUMENT=&__VIEWSTATE=%2FwEPDwUKMTQ3MDYzNjA4Mw9kFgICAQ9kFgQCAQ9kFgICBA8WAh4LXyFJdGVtQ291bnQCBRYKZg9kFgJmDxUCCeWMluWmhuWTgQnljJblpoblk4FkAgEPZBYCZg8VAgblm77kuaYG5Zu%2B5LmmZAICD2QWAmYPFQID6Z6LA%2Bmei2QCAw9kFgJmDxUCBuecvOmVnAbnnLzplZxkAgQPZBYCZg8VAgbljIXljIUG5YyF5YyFZAIDD2QWAgIBDxYCHgRUZXh0BXM8YSBocmVmPSJodHRwOi8vd3d3Lm1haWRvbmd4aS5jb20iIHRpdGxlPSLpppbpobUiPummlumhtTwvYT4gJmd0OyA8YSBocmVmPSIjIiB0aXRsZT0i55So5oi355m76ZmGIj7nlKjmiLfnmbvpmYY8L2E%2BZBgBBR5fX0NvbnRyb2xzUmVxdWlyZVBvc3RCYWNrS2V5X18WAQULY2hrVHdvV2Vla3NivmvxNC3%2FW7szjW5MgMTseXm5rQ%3D%3D&head%24txtSearch=%E8%AF%B7%E8%BE%93%E5%85%A5%E6%90%9C%E7%B4%A2%E5%85%B3%E9%94%AE%E5%AD%97&head%24txtSearch1=%E8%AF%B7%E8%BE%93%E5%85%A5%E6%90%9C%E7%B4%A2%E5%85%B3%E9%94%AE%E5%AD%97&head%24txtSearch2=%E8%AF%B7%E8%BE%93%E5%85%A5%E6%90%9C%E7%B4%A2%E5%85%B3%E9%94%AE%E5%AD%97&head%24txtSearch3=%E8%AF%B7%E8%BE%93%E5%85%A5%E6%90%9C%E7%B4%A2%E5%85%B3%E9%94%AE%E5%AD%97&txusername=xx&txuserpwd=xx&checkcod=" + textBox3.Text.Trim() + "&textbox23=3311"; request = (HttpWebRequest)WebRequest.Create("http://ucenter.maidongxi.com/login.aspx"); request.CookieContainer = cc; request.KeepAlive = true; request.AllowAutoRedirect = false; //MessageBox.Show(cookiestr); request.Headers.Add("Cookie:" + cookiestr); byte[] postdataByte = Encoding.UTF8.GetBytes(postdata); request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = postdataByte.Length; request.Method = "POST"; Stream st; st = request.GetRequestStream(); st.Write(postdataByte, 0, postdataByte.Length); st.Close(); response = (HttpWebResponse)request.GetResponse(); StreamReader readd = new StreamReader(response.GetResponseStream(), Encoding.UTF8); string context = readd.ReadToEnd(); readd.Close(); response.Close(); cookiestr = request.CookieContainer.GetCookieHeader(request.RequestUri); local = context.Split(new char[] { '"' })[1]; //MessageBox.Show("第一次跳转的页面是:" + local); //跳转页面开始 request = (HttpWebRequest)WebRequest.Create("http://ucenter.maidongxi.com/Buyer/UserInfo.aspx"); request.Method = "GET"; request.CookieContainer = cc; request.Headers.Add("Cookie:" + cookiestr); request.KeepAlive = true; response = (HttpWebResponse)request.GetResponse(); readd = new StreamReader(response.GetResponseStream(), Encoding.UTF8); context = readd.ReadToEnd(); if (context.IndexOf("退出") != -1) { textBox1.Text = "登录成功了"; MessageBox.Show("登录成功了"); } else { textBox1.Text = "没有登录成功"; MessageBox.Show("登录未成功了"); } //cookiestr += cookiestr; //开始抢之前要get页面 request = (HttpWebRequest)WebRequest.Create("http://www.maidongxi.com/Coupon"); request.Method = "GET"; request.CookieContainer = cc; request.Headers.Add("Cookie:" + cookiestr); request.KeepAlive = true; response = (HttpWebResponse)request.GetResponse(); cookiestr = request.CookieContainer.GetCookieHeader(request.RequestUri)+";"+cookiestr; response.Close(); request.Abort(); postdata = "method=M_PanDuan&couponid=b2d5e678-d404-4225-87ca-44c426e01ba7&starttime=2011-6-21+10%3A30%3A00"; postdataByte = Encoding.UTF8.GetBytes(postdata); request = (HttpWebRequest)WebRequest.Create("http://www.maidongxi.com/aas/Index.ashx"); request.Method = "POST"; request.CookieContainer = cc; request.KeepAlive = true; request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8"; request.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1"; request.Referer = "http://www.maidongxi.com/Coupon"; request.Headers.Add("X-Requested-With:XMLHttpRequest"); request.ContentLength = postdataByte.Length; request.Headers.Add("Cookie:" + cookiestr); st = request.GetRequestStream(); st.Write(postdataByte, 0, postdataByte.Length); st.Close(); response = (HttpWebResponse)request.GetResponse(); readd = new StreamReader(response.GetResponseStream(), Encoding.UTF8); context = readd.ReadToEnd(); readd.Close(); response.Close(); textBox4.Text =DateTime.Now.ToString()+ context; #region 异步请求 //request.BeginGetRequestStream(new AsyncCallback(ReadCallback), request); 维持主线程继续直到异步操作完成。 //alldone.WaitOne(); 获取响应 //response = (HttpWebResponse)request.GetResponse(); 读取响应流 //readd = new StreamReader(response.GetResponseStream()); 读取响应流 //context = readd.ReadToEnd(); //MessageBox.Show(context); //readd.Close(); //response.Close(); #endregion } private void ReadCallback(IAsyncResult asynchronousResult) { try { HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState; //结束请求操作 //结束对用于写入数据的System.IO.Stream 对象的异步请求。 Stream postStream = request.EndGetRequestStream(asynchronousResult); string postData = textBox4.Text.Trim(); //将字符串转化为字节数组 byte[] byteArray = Encoding.UTF8.GetBytes(postData); //向请求流中写入字节 postStream.Write(byteArray, 0, postData.Length); postStream.Close(); postStream.Dispose(); alldone.Set(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void button4_Click(object sender, EventArgs e) { string postdata = textBox4.Text.Trim(); byte[] postdataByte = Encoding.UTF8.GetBytes(postdata); request = (HttpWebRequest)WebRequest.Create("http://www.maidongxi.com/aas/Control/QHead_N.ashx"); request.Method = "POST"; request.CookieContainer = cc; request.KeepAlive = true; request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8"; request.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1"; request.Referer = "http://www.maidongxi.com/Coupon"; //request.Headers.Add("x-requested-with:XMLHttpRequest"); request.ContentLength = postdataByte.Length; request.Headers.Add("Cookie:" +textBox2.Text.Trim()); Stream st; st = request.GetRequestStream(); st.Write(postdataByte, 0, postdataByte.Length); st.Close(); response = (HttpWebResponse)request.GetResponse(); Stream responseStream = response.GetResponseStream(); StreamReader readd = new StreamReader(response.GetResponseStream(), Encoding.UTF8); string context = readd.ReadToEnd(); responseStream.Close(); readd.Close(); MessageBox.Show(context); } } }

 

 

 

你可能感兴趣的:(WebRequest 模拟请求登录 终于搞定了!)