c# winform實現網頁上使用者自動登陸,類比網站登入

來源:互聯網
上載者:User
using System; using System.Collections.Generic; using System.Text; using System.Net; using System.IO;namespace Czt.Web {     /// <summary>     /// 實現網站登入類     /// </summary>     public class Post     {         /// <summary>         /// 網站Cookies         /// </summary>         private string _cookieHeader = string.Empty;         public string CookieHeader         {             get             {                 return _cookieHeader;             }             set             {                 _cookieHeader = value;             }         }         /// <summary>         /// 網站編碼         /// </summary>         private string _code = string.Empty;         public string Code         {             get { return _code; }             set { _code = value; }         }         private string _pageContent = string.Empty;         public string PageContent         {             get { return _pageContent; }             set { _pageContent = value; }         }        private Dictionary<string, string> _para = new Dictionary<string, string>();         public Dictionary<string, string> Para         {             get { return _para; }             set { _para = value; }         }         /**/         /// <summary>         /// 功能描述:類比登入頁面,提交登入資料進行登入,並記錄Header中的cookie         /// </summary>         /// <param name="strURL">登入資料提交的頁面地址</param>         /// <param name="strArgs">使用者登入資料</param>         /// <param name="strReferer">引用地址</param>         /// <param name="code">網站編碼</param>         /// <returns>可以返回頁面內容或不返回</returns>         public string PostData(string strURL, string strArgs, string strReferer, string code, string method)         {             return  PostData(strURL,  strArgs,  strReferer,  code,  method, string.Empty);         }         public string PostData(string strURL, string strArgs, string strReferer, string code, string method, string contentType)         {             try             {                 string strResult = "";                 HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(strURL);                 myHttpWebRequest.AllowAutoRedirect = true;                 myHttpWebRequest.KeepAlive = true;                 myHttpWebRequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/msword, application/x-shockwave-flash, */*";                 myHttpWebRequest.Referer = strReferer;                 myHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 2.0.50727)";                if (string.IsNullOrEmpty(contentType))                 {                     myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";                 }                 else                 {                     myHttpWebRequest.ContentType = "contentType";                 }                myHttpWebRequest.Method = method;                myHttpWebRequest.Headers.Add("Accept-Encoding", "gzip, deflate");                if (myHttpWebRequest.CookieContainer == null)                 {                     myHttpWebRequest.CookieContainer = new CookieContainer();                 }                if (this.CookieHeader.Length > 0)                 {                     myHttpWebRequest.Headers.Add("cookie:" + this.CookieHeader);                     myHttpWebRequest.CookieContainer.SetCookies(new Uri(strURL), this.CookieHeader);                 }                 byte[] postData = Encoding.GetEncoding(code).GetBytes(strArgs);                 myHttpWebRequest.ContentLength = postData.Length;                System.IO.Stream PostStream = myHttpWebRequest.GetRequestStream();                 PostStream.Write(postData, 0, postData.Length);                 PostStream.Close();                HttpWebResponse response = null;                 System.IO.StreamReader sr = null;                 response = (HttpWebResponse)myHttpWebRequest.GetResponse();                 if (myHttpWebRequest.CookieContainer != null)                 {                     this.CookieHeader = myHttpWebRequest.CookieContainer.GetCookieHeader(new Uri(strURL));                 }                sr = new System.IO.StreamReader(response.GetResponseStream(), Encoding.GetEncoding(code));    //    //utf-8                 strResult = sr.ReadToEnd();                 sr.Close();                 response.Close();                 return strResult;             }             catch (Exception ex)             {                 Utilities.Document.Create("C:\\error.log", strArgs, true, Encoding.UTF8);             }             return string.Empty;         }        /**/         /// <summary>         /// 功能描述:在PostLogin成功登入後記錄下Headers中的cookie,然後擷取此網站上其他頁面的內容         /// </summary>         /// <param name="strURL">擷取網站的某頁面的地址</param>         /// <param name="strReferer">引用的地址</param>         /// <returns>返回頁面內容</returns>         public string GetPage(string strURL, string strReferer, string code)         {             return GetPage(strURL, strReferer,code,string.Empty);         }         public string GetPage(string strURL, string strReferer,string code,string contentType)         {             string strResult = "";             HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(strURL);             myHttpWebRequest.AllowAutoRedirect = true;             myHttpWebRequest.KeepAlive = false;             myHttpWebRequest.Accept = "*/*";             myHttpWebRequest.Referer = strReferer;             myHttpWebRequest.Headers.Add("Accept-Encoding", "gzip, deflate");            myHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 2.0.50727)";             if (string.IsNullOrEmpty(contentType))             {                 myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";             }             else             {                 myHttpWebRequest.ContentType = contentType;             }             myHttpWebRequest.Method = "GET";            if (myHttpWebRequest.CookieContainer == null)             {                 myHttpWebRequest.CookieContainer = new CookieContainer();             }            if (this.CookieHeader.Length > 0)             {                 myHttpWebRequest.Headers.Add("cookie:" + this.CookieHeader);                 myHttpWebRequest.CookieContainer.SetCookies(new Uri(strURL), this.CookieHeader);             }             HttpWebResponse response = null;             System.IO.StreamReader sr = null;             response = (HttpWebResponse)myHttpWebRequest.GetResponse();             Stream streamReceive;             string gzip = response.ContentEncoding;            if (string.IsNullOrEmpty(gzip) || gzip.ToLower() != "gzip")             {                 streamReceive = response.GetResponseStream();             }             else             {                 streamReceive = new System.IO.Compression.GZipStream(response.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress);             }            sr = new System.IO.StreamReader(streamReceive, Encoding.GetEncoding(code));            if (response.ContentLength > 1)             {                 strResult = sr.ReadToEnd();             }             else             {                 char[] buffer=new char[256];                 int count = 0;                 StringBuilder sb = new StringBuilder();                 while ((count = sr.Read(buffer, 0, buffer.Length)) > 0)                 {                     sb.Append(new string(buffer));                 }                 strResult = sb.ToString();             }             sr.Close();             response.Close();             return strResult;         }    } } 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.