using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Net; namespace LoginWeb { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { HttpRec myrec = new HttpRec(); string PostDate = "txtUserAct=69022565&txtUserPwd=69022565&authencode="; PostDate += myrec.GetCode(); CookieCollection c = myrec.ReturnCookies(); foreach (Cookie cook in c) { this.richTextBox1.Text += cook.Domain+"--"+cook.Name+"---"+cook.Value; } this.richTextBox1.Text +=PostDate+ myrec.LoginWeb(PostDate); } } public class HttpRec { private CookieCollection _cookCollection; private string _Url = "http://59.50.113.196:9080/personal/servlet/LoginServlet"; private string _refUrl = "http://59.50.113.196:9080/personal/interfaces/hainan/index.jsp"; private string _strErr; public CookieCollection ReturnCookies() { //if (_cookCollection != null) //{ //} return _cookCollection; } public string GetCode() { HttpWebRequest rqq = (HttpWebRequest)HttpWebRequest.Create("Http://59.50.113.196:9080/security/servlet/AuthenCodeImage"); rqq.Method = "Get"; rqq.KeepAlive = true; // rqq.Referer = _refUrl; if (rqq.CookieContainer == null) { rqq.CookieContainer = new CookieContainer(); } HttpWebResponse rpp = (HttpWebResponse)rqq.GetResponse(); Image img = Bitmap.FromStream(rpp.GetResponseStream()); _cookCollection = rpp.Cookies; // img.Save(Environment.CurrentDirectory+"//1.bmp",System.Drawing.Imaging.ImageFormat.Bmp); return ChuLiImg((Bitmap)img); } public static string ChuLiImg(Bitmap img) { Rectangle[] RcArr = new Rectangle[4]; Rectangle RC1 = new Rectangle(4, 4, 6, 9); Rectangle RC2 = new Rectangle(11, 4, 6, 9); Rectangle RC3 = new Rectangle(18, 4, 6, 9); Rectangle RC4 = new Rectangle(25, 4, 6, 9); RcArr[0] = RC1; RcArr[1] = RC2; RcArr[2] = RC3; RcArr[3] = RC4; Bitmap[] bit = new Bitmap[4]; string CodeStr = string.Empty; for (int i = 0; i < 4; i++) { string str =GetImgStr(ClearBg(img), RcArr[i]); bit[i] = ((Bitmap)img).Clone(RcArr[i], img.PixelFormat); if (str == "0111111111111111100000001000000011111111") { CodeStr += "0"; } if (str == "0000000000110000011000001111111111111111") { CodeStr += "1"; } if (str == "0100000111000011100001101000110011111000") { CodeStr += "2"; } if (str == "0100000111000001100100001001000011111111") { CodeStr += "3"; } if (str == "0000011000011010011000101111111111111111") { CodeStr += "4"; } if (str == "0011100111111001110100001001000010011111") { CodeStr += "5"; } if (str == "0011111101111111100100001001000011011111") { CodeStr += "6"; } if (str == "1000000010000011100011111011110011110000") { CodeStr += "7"; } if (str == "0111011111111111100010001000100011111111") { CodeStr += "8"; } if (str == "0111100111111101100001001000010011111111") { CodeStr += "9"; } } return CodeStr; } public static string GetImgStr(Bitmap Bigimg, Rectangle Rc) { Bitmap RCBmp = Bigimg.Clone(Rc, Bigimg.PixelFormat); string str = ""; for (int w = 0; w < Rc.Width - 1; w++) { for (int h = 0; h < Rc.Height - 1; h++) { if (RCBmp.GetPixel(w, h).ToArgb() == Color.Black.ToArgb()) { str += "1"; } else { str += "0"; } } } return str; } private static Bitmap ClearBg(Image Img) { Bitmap Smallimg = new Bitmap(Img); for (int w = 0; w < Smallimg.Width - 1; w++) { for (int h = 0; h < Smallimg.Height - 1; h++) { if (Smallimg.GetPixel(w, h).ToArgb() < (Color.Black.ToArgb() * 0.4)) { Smallimg.SetPixel(w, h, Color.Black); } else { Smallimg.SetPixel(w, h, Color.White); } } } return Smallimg; } public string LoginWeb(string PostData) { string str = string.Empty; HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(_Url);//创建req req.Accept = "*/*"; //接受任意文件 req.UserAgent = " Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; EmbeddedWB 14.52 from: http://www.bsalsa.com/ EmbeddedWB 14.52; .NET CLR 2.0.50727)"; // 模拟使用IE在浏览 // req.KeepAlive = true; req.Referer = "http://59.50.113.196:9080/personal/interfaces/hainan/index.jsp"; if (req.CookieContainer == null) { req.CookieContainer = new CookieContainer(); } if (this._cookCollection != null) { System.Uri u = new Uri("http://59.50.113.196:9080/personal/servlet/LoginServlet"); //doenet处理cookie的bug:请求的服务器和cookie的Host必须一直,否则不发送或获取! //这里修改成一致! foreach (System.Net.Cookie c in _cookCollection) { c.Domain = u.Host; } req.CookieContainer.Add(_cookCollection); } req.CookieContainer.Add(_cookCollection); // req.CookieContainer.SetCookies(new Uri("59.50.113.196"),_cookCollection["d"].Value); if (PostData != null & PostData.Length > 0) { req.Method = "POST"; req.ContentType = "application/x-www-form-urlencoded"; byte[] b = Encoding.Default.GetBytes(PostData); req.ContentLength = b.Length; req.AutomaticDecompression = DecompressionMethods.GZip; System.IO.Stream sw = null; try { sw = req.GetRequestStream(); sw.Write(b, 0, b.Length); } catch (System.Exception ex) { this._strErr = ex.Message; } finally { if (sw != null) { sw.Close(); } } } HttpWebResponse rep = null; System.IO.StreamReader sr = null; try { rep = (HttpWebResponse)req.GetResponse(); _cookCollection = rep.Cookies; sr = new System.IO.StreamReader(rep.GetResponseStream(), Encoding.Default); str = sr.ReadToEnd(); if (sr != null) { sr.Close(); } } catch (Exception e) { MessageBox.Show(e.Message); } return str; } } }
模拟网站登陆
最新推荐文章于 2022-07-29 16:37:42 发布