远程登陆,带验证码
通过一个winform 界面实现远程登录网站 带验证码,别的不说了,下附开放代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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.Text.RegularExpressions; using System.Threading; namespace FLY.SZSB { public partial class Form1 : Form { public Form1() { InitializeComponent(); } Fly.WebLogo.UrlLogoin login = new Fly.WebLogo.UrlLogoin(); private void button1_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(this.txtID.Text.Trim())) { MessageBox.Show("身证不能为空"); this.txtID.Focus(); return; } else if (string.IsNullOrEmpty(this.txtComputer.Text.Trim())) { MessageBox.Show("电脑号不能为空"); this.txtComputer.Focus(); return; } else if (string.IsNullOrEmpty(this.txtCode.Text.Trim())) { MessageBox.Show("验证码不能为空"); this.txtCode.Focus(); return; } string url = "http://wssb2.szsi.gov.cn/NetApplyWeb/personacctoutResult.jsp"; string post = string.Format("id={0}&bacode={1}&PSINPUT={2}", this.txtID.Text, this.txtComputer.Text, this.txtCode.Text.Trim()); oneData = Encoding.Default.GetBytes(post); Uri uri = new Uri(url); myHttpWebRequest = (HttpWebRequest)WebRequest.Create(uri);//请求的URL myHttpWebRequest.CookieContainer = Cookies;//*发送COOKIE myHttpWebRequest.Method = "POST"; myHttpWebRequest.ContentType = "application/x-www-form-urlencoded"; myHttpWebRequest.ContentLength = oneData.Length; Stream newMyStream = myHttpWebRequest.GetRequestStream(); newMyStream.Write(oneData, 0, oneData.Length); try { HttpWebResponse response = (HttpWebResponse)myHttpWebRequest.GetResponse(); StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.Default); string str = sr.ReadToEnd(); string msg = string.Empty; if (!Test(str, out msg)) { this.lblResult.Text = msg; } else { LoadResult(str); } } catch (Exception ex) { this.lblResult.Text = ex.Message; } } //判断错误 public bool Test(string str, out string msg) { msg = ""; if (str.IndexOf("验证码不正确") > -1) { msg = "验证码不正确"; this.txtCode.Focus(); return false; } else { return true; } } //显示结果 private void LoadResult(string str) { if (str.IndexOf("没有查询到数据") > 0) { this.lblResult.Text = "没有查询到数据"; } else if (str.IndexOf("参保情况") > 0) { List<string> list = GetHtmlImageUrlList(str); StringBuilder sb = new StringBuilder("参保情况:\r\n"); for (int i = 0; i < list.Count; i++) { if ((i + 1) % 2 == 0) { sb.Append(list[i] + "\r\n"); } else { sb.Append(list[i]); } } this.lblResult.Text = sb.ToString(); } } private void button2_Click(object sender, EventArgs e) { List<string> list = GetHtmlImageUrlList("<table border='0' cellpadding='1' cellspacing='1' width='660'><tr height='19' bgcolor='#fbecbb'><td colspan='4' height='19'>参保情况</td></tr><tr height='19' bgcolor='#d2eeff'><td height='19' width='161' align='right'>当前缴费状态标记:</td><td height='19' width='185' align='left' colspan='3'>正常</td><td height='19' width='185' align='left' colspan='3'>正常</td></tr></table>"); StringBuilder sb = new StringBuilder(); for (int i = 0; i < list.Count; i++) { if ((i + 1) % 2 == 0) { sb.Append(list[i] + "\r\n"); } else { sb.Append(list[i]); } } this.lblResult.Text = sb.ToString(); } /// <summary> /// 取得HTML中所有图片的 URL。 /// </summary> /// <param name="sHtmlText">HTML代码</param> /// <returns>图片的URL列表</returns> public List<string> GetHtmlImageUrlList(string sHtmlText) { sHtmlText = sHtmlText.Replace("\"", "'"); // Regex regImg = new Regex(@"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase); // 定义正则表达式用来匹配 img 标签 string reg = @"<td[^>].*?>(?<content>[^<>]+)</td>"; Regex regImg = new Regex(reg, RegexOptions.IgnoreCase); // 搜索匹配的字符串 MatchCollection matches = regImg.Matches(sHtmlText); List<string> sUrlList = new List<string>(); // 取得匹配项列表 foreach (Match match in matches) { string temp = match.Result("$1").Replace("\r\n", "").Replace("\t", "").Trim(); if (temp.IndexOf("nbsp") == -1) { sUrlList.Add(temp); } } return sUrlList; } //加载验证码 private void Form1_Load(object sender, EventArgs e) { Thread t1 = new Thread(new ThreadStart(LoadCookie)); t1.Start(); Thread t2 = new Thread(new ThreadStart(LoadCodeImg)); t2.Start(); } private void LoadCodeImg() { try { string oneUrl = "http://wssb2.szsi.gov.cn/NetApplyWeb/CImages"; myHttpWebRequest = (HttpWebRequest)WebRequest.Create(oneUrl);//请求的URL myHttpWebRequest.CookieContainer = Cookies;//*发送COOKIE myHttpWebRequest.Method = "GET"; myHttpWebRequest.ContentType = "application/x-www-form-urlencoded"; //获取返回资源 HttpWebResponse response = (HttpWebResponse)myHttpWebRequest.GetResponse(); //获取流 Image bitmapImage = Bitmap.FromStream(response.GetResponseStream()) as Bitmap; this.pictureBox1.Image = bitmapImage; } catch (Exception ex) { //if (this.lblResult.InvokeRequired) //{ // SetLabelTextDelegate sss = new SetLabelTextDelegate(SetLabelText); // this.Invoke(sss, ex.Message); //} //else //{ // this.lblResult.Text = ex.Message; //} } } //更改验证码 private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { Thread t3 = new Thread(new ThreadStart(LoadCodeImg)); t3.Start(); this.lblResult.Text = ""; } CookieContainer Cookies = new CookieContainer(); HttpWebRequest myHttpWebRequest; byte[] oneData = { }; /// <summary> /// 加载Cookie /// </summary> private void LoadCookie() { try { string oneUrl = "http://wssb2.szsi.gov.cn/NetApplyWeb/personacctoutInput.jsp"; myHttpWebRequest = (HttpWebRequest)WebRequest.Create(oneUrl);//请求的URL myHttpWebRequest.CookieContainer = Cookies;//*发送COOKIE myHttpWebRequest.Method = "POST"; myHttpWebRequest.ContentType = "application/x-www-form-urlencoded"; myHttpWebRequest.ContentLength = oneData.Length; Stream newMyStream = myHttpWebRequest.GetRequestStream(); newMyStream.Write(oneData, 0, oneData.Length); } catch (Exception ex) { //if (this.lblResult.InvokeRequired) //{ // SetLabelTextDelegate sss = new SetLabelTextDelegate(SetLabelText); // this.Invoke(sss, ex.Message); //} //else //{ // this.lblResult.Text = ex.Message; //} } } delegate void SetLabelTextDelegate(Label lb1, string txt); private void SetLabelText(Label lab, string msg) { lab.Text = msg; } } }