1. 程式人生 > >winform模擬登陸(帶驗證碼)

winform模擬登陸(帶驗證碼)

       CookieContainer cookies = new CookieContainer();

       //驗證碼
        public static Image doGetImg(CookieContainer bCookie)
        {

            try

            {
                string url = "http://";//驗證碼網址
                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url.ToString());
                myRequest.ServicePoint.Expect100Continue = true;
                myRequest.CookieContainer = bCookie;
                myRequest.Method = "GET";
                myRequest.Timeout = 30000;
                myRequest.KeepAlive = true;
                myRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36";
                myRequest.ContentType = "application/x-www-form-urlencoded";
                HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
                return Bitmap.FromStream(myResponse.GetResponseStream());
            }
            catch
            {
                return null;
            }
        }

//登入

public string doPost(CookieContainer bCookie)
        {
            try
            {
                string Url = "http://";//請求網址
                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(Url.ToString());
                myRequest.CookieContainer = bCookie;
                myRequest.Referer = "http://";//引用網址
                myRequest.Method = "POST";
                myRequest.Timeout = 30000;
                myRequest.KeepAlive = true;
                myRequest.Headers["Cache-control"] = "no-cache";
                myRequest.Headers["Accept-Language"] = "zh-cn";
                myRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36";
                myRequest.ContentType = "application/x-www-form-urlencoded";
                myRequest.Accept = "*/*";
                string poststr = "__VIEWSTATE=%2FwEPDwUKMTg4NTQ0NDYzNWRkN9lElDjzvutr%2Fbh%2Bfa8%2B6a3sX2YR1jJ%2F4ZqhN2RhwCw%3D&__EVENTVALIDATION=%2FwEWBgLc%2B5HGBgKl1bKzCQKd%2B7qdDgLChPzDDQL07onSBgKgt7D9CtkAQM2nDOH0B%2BGNB1FadPmeXm4eRkF3K4BcsHI3U6Io&UserName=" + UserName.Text + "&Pwd=" + Pwd.Text + "&Code=" + Code.Text + "";
                byte[] data = Encoding.UTF8.GetBytes(poststr);
                myRequest.ContentLength = data.Length;
                Stream newStream = myRequest.GetRequestStream();
                newStream.Write(data, 0, data.Length);
                newStream.Close();
                HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
                StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
                string outdata = reader.ReadToEnd();
                reader.Close();
                if (outdata.Contains("該使用者不存在"))
                {
                    MessageBox.Show("該使用者不存在");
                }
                return outdata;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }