咱們常常會碰到須要程序模擬登陸一個網站,那若是網站須要填寫驗證碼的要怎樣模擬登陸呢?
這篇文章利用了 HttpWebRequest 和 HttpWebResponse 模擬登陸了有驗證碼的網站。html
程序設計的界面很簡單,三個TextBox分別輸入用戶名、密碼和驗證碼,一個Image控件顯示從網站請求到的驗證碼圖片,還有兩個按鈕,一個換驗證碼,一個登陸。瀏覽器
寫程序前,先用瀏覽器的開發者工具觀察下登陸頁面有什麼請求,我這裏用的是 FireBug,下面兩個圖是在 FireBug 的網絡面板中截的。cookie
能夠看到打開登陸頁面時有個 GET 請求驗證碼的,在 FireBug 中能夠看到:網絡
上面的圖上能夠看到這一句: Set-Cookie GUID=c89eabb62d9d4f35b491a8afd371b4ad; path=/app
這裏請求的驗證碼頁面保存了一個Cookie。工具
而後咱們輸入驗證碼,點擊「登陸」的時候有個 POST 請求,在 FireBug 能夠看到:post
這裏的重點是這句:CodeStatus=&bkurl=&companyid=&username=test&password=test&Validate=yyxe
字體
從這裏咱們能夠看到用戶名、密碼還有驗證碼提交的方式。網站
下面大概說下程序的步驟:url
1. 請求驗證碼,顯示在程序界面上,而且保存Cookie。
2. 提交姓名、密碼和驗證碼數據,得到響應。
我這裏是WPF程序,若是是Winform也相似。
完整代碼以下:
CookieContainer cookies = null; string strCookies = string.Empty; private void btnChangeValidate_Click(object sender, RoutedEventArgs e) { GetValidateImage(); } /// <summary> /// 獲取驗證碼和Cookie /// </summary> private void GetValidateImage() { cookies = new CookieContainer(); string url = "http://******/picturetimestamp.asp"; //驗證碼頁面 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Accept = "*/*"; request.Method = "GET"; request.UserAgent = "Mozilla/5.0"; request.CookieContainer = new CookieContainer(); //暫存到新實例 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); MemoryStream ms = null; using (var stream = response.GetResponseStream()) { Byte[] buffer = new Byte[response.ContentLength]; int offset = 0, actuallyRead = 0; do { actuallyRead = stream.Read(buffer, offset, buffer.Length - offset); offset += actuallyRead; } while (actuallyRead > 0); ms = new MemoryStream(buffer); } response.Close(); cookies = request.CookieContainer; //保存cookies strCookies = request.CookieContainer.GetCookieHeader(request.RequestUri); //把cookies轉換成字符串 BitmapImage bi = new BitmapImage(); bi.BeginInit(); bi.StreamSource = (Stream)ms; bi.EndInit(); imgValidate.Source = bi; } private void btnLogin_Click(object sender, RoutedEventArgs e) { Login(); } /// <summary> /// 登陸 /// </summary> /// <returns></returns> private string Login() { HttpWebRequest request = null; string url = "http://******/loginproc.asp"; //登陸頁面 request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.Accept = "*/*;"; request.UserAgent = "Mozilla/5.0"; request.ContentType = "application/x-www-form-urlencoded"; request.AllowAutoRedirect = true; request.CookieContainer = cookies; request.KeepAlive = true; string postData = string.Format("username={0}&password={1}&Validate={2}&isautologin=1&Submit=", txtUserName.Text, txtPassword.Text, txtValidate.Text); //這裏按照前面FireBug中查到的POST字符串作相應修改。 byte[] postdatabyte = Encoding.UTF8.GetBytes(postData); request.ContentLength = postdatabyte.Length; using (Stream stream = request.GetRequestStream()) { stream.Write(postdatabyte, 0, postdatabyte.Length); } HttpWebResponse response = (HttpWebResponse)request.GetResponse(); string strWebData = string.Empty; using (StreamReader reader = new StreamReader(response.GetResponseStream())) { strWebData = reader.ReadToEnd(); } return strWebData; }
但願能夠幫到有須要的人。