c# 实现网页上用户自动登陆|asp.net 模拟网站登录

 
  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.Text;   
  4. using System.Net;   
  5. using System.IO;  
  6.   
  7. namespace Czt.Web   
  8. {   
  9.     /// <summary>   
  10.     /// 实现网站登录类   
  11.     /// </summary>   
  12.     public class Post   
  13.     {   
  14.         /// <summary>   
  15.         /// 网站Cookies   
  16.         /// </summary>   
  17.         private string _cookieHeader = string.Empty;   
  18.         public string CookieHeader   
  19.         {   
  20.             get   
  21.             {   
  22.                 return _cookieHeader;   
  23.             }   
  24.             set   
  25.             {   
  26.                 _cookieHeader = value;   
  27.             }   
  28.         }   
  29.         /// <summary>   
  30.         /// 网站编码   
  31.         /// </summary>   
  32.         private string _code = string.Empty;   
  33.         public string Code   
  34.         {   
  35.             get { return _code; }   
  36.             set { _code = value; }   
  37.         }  
  38.   
  39.    
  40.         private string _pageContent = string.Empty;   
  41.         public string PageContent   
  42.         {   
  43.             get { return _pageContent; }   
  44.             set { _pageContent = value; }   
  45.         }  
  46.   
  47.         private Dictionary<stringstring> _para = new Dictionary<stringstring>();   
  48.         public Dictionary<stringstring> Para   
  49.         {   
  50.             get { return _para; }   
  51.             set { _para = value; }   
  52.         }  
  53.   
  54.    
  55.         /**/   
  56.         /// <summary>   
  57.         /// 功能描述:模拟登录页面,提交登录数据进行登录,并记录Header中的cookie   
  58.         /// </summary>   
  59.         /// <param name="strURL">登录数据提交的页面地址</param>   
  60.         /// <param name="strArgs">用户登录数据</param>   
  61.         /// <param name="strReferer">引用地址</param>   
  62.         /// <param name="code">网站编码</param>   
  63.         /// <returns>可以返回页面内容或不返回</returns>   
  64.         public string PostData(string strURL, string strArgs, string strReferer, string code, string method)   
  65.         {   
  66.             return  PostData(strURL,  strArgs,  strReferer,  code,  method, string.Empty);   
  67.         }   
  68.         public string PostData(string strURL, string strArgs, string strReferer, string code, string method, string contentType)   
  69.         {   
  70.             try   
  71.             {   
  72.                 string strResult = "";   
  73.                 HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(strURL);   
  74.                 myHttpWebRequest.AllowAutoRedirect = true;   
  75.                 myHttpWebRequest.KeepAlive = true;   
  76.                 myHttpWebRequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/msword, application/x-shockwave-flash, */*";   
  77.                 myHttpWebRequest.Referer = strReferer;  
  78.   
  79.    
  80.                 myHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 2.0.50727)";  
  81.   
  82.                 if (string.IsNullOrEmpty(contentType))   
  83.                 {   
  84.                     myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";   
  85.                 }   
  86.                 else   
  87.                 {   
  88.                     myHttpWebRequest.ContentType = "contentType";   
  89.                 }  
  90.   
  91.                 myHttpWebRequest.Method = method;  
  92.   
  93.                 myHttpWebRequest.Headers.Add("Accept-Encoding""gzip, deflate");  
  94.   
  95.                 if (myHttpWebRequest.CookieContainer == null)   
  96.                 {   
  97.                     myHttpWebRequest.CookieContainer = new CookieContainer();   
  98.                 }  
  99.   
  100.                 if (this.CookieHeader.Length > 0)   
  101.                 {   
  102.                     myHttpWebRequest.Headers.Add("cookie:" + this.CookieHeader);   
  103.                     myHttpWebRequest.CookieContainer.SetCookies(new Uri(strURL), this.CookieHeader);   
  104.                 }  
  105.   
  106.    
  107.   
  108.                 byte[] postData = Encoding.GetEncoding(code).GetBytes(strArgs);   
  109.                 myHttpWebRequest.ContentLength = postData.Length;  
  110.   
  111.                 System.IO.Stream PostStream = myHttpWebRequest.GetRequestStream();   
  112.                 PostStream.Write(postData, 0, postData.Length);   
  113.                 PostStream.Close();  
  114.   
  115.                 HttpWebResponse response = null;   
  116.                 System.IO.StreamReader sr = null;   
  117.                 response = (HttpWebResponse)myHttpWebRequest.GetResponse();  
  118.   
  119.    
  120.   
  121.                 if (myHttpWebRequest.CookieContainer != null)   
  122.                 {   
  123.                     this.CookieHeader = myHttpWebRequest.CookieContainer.GetCookieHeader(new Uri(strURL));   
  124.                 }  
  125.   
  126.                 sr = new System.IO.StreamReader(response.GetResponseStream(), Encoding.GetEncoding(code));    //    //utf-8   
  127.                 strResult = sr.ReadToEnd();   
  128.                 sr.Close();   
  129.                 response.Close();   
  130.                 return strResult;   
  131.             }   
  132.             catch (Exception ex)   
  133.             {   
  134.                 Utilities.Document.Create("C:\\error.log", strArgs, true, Encoding.UTF8);   
  135.             }   
  136.             return string.Empty;   
  137.         }  
  138.   
  139.         /**/   
  140.         /// <summary>   
  141.         /// 功能描述:在PostLogin成功登录后记录下Headers中的cookie,然后获取此网站上其他页面的内容   
  142.         /// </summary>   
  143.         /// <param name="strURL">获取网站的某页面的地址</param>   
  144.         /// <param name="strReferer">引用的地址</param>   
  145.         /// <returns>返回页面内容</returns>   
  146.         public string GetPage(string strURL, string strReferer, string code)   
  147.         {   
  148.             return GetPage(strURL, strReferer,code,string.Empty);   
  149.         }   
  150.         public string GetPage(string strURL, string strReferer,string code,string contentType)   
  151.         {   
  152.             string strResult = "";   
  153.             HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(strURL);   
  154.             myHttpWebRequest.AllowAutoRedirect = true;   
  155.             myHttpWebRequest.KeepAlive = false;   
  156.             myHttpWebRequest.Accept = "*/*";   
  157.             myHttpWebRequest.Referer = strReferer;   
  158.             myHttpWebRequest.Headers.Add("Accept-Encoding""gzip, deflate");  
  159.   
  160.             myHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 2.0.50727)";   
  161.             if (string.IsNullOrEmpty(contentType))   
  162.             {   
  163.                 myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";   
  164.             }   
  165.             else   
  166.             {   
  167.                 myHttpWebRequest.ContentType = contentType;   
  168.             }   
  169.             myHttpWebRequest.Method = "GET";  
  170.   
  171.             if (myHttpWebRequest.CookieContainer == null)   
  172.             {   
  173.                 myHttpWebRequest.CookieContainer = new CookieContainer();   
  174.             }  
  175.   
  176.             if (this.CookieHeader.Length > 0)   
  177.             {   
  178.                 myHttpWebRequest.Headers.Add("cookie:" + this.CookieHeader);   
  179.                 myHttpWebRequest.CookieContainer.SetCookies(new Uri(strURL), this.CookieHeader);   
  180.             }  
  181.   
  182.    
  183.             HttpWebResponse response = null;   
  184.             System.IO.StreamReader sr = null;   
  185.             response = (HttpWebResponse)myHttpWebRequest.GetResponse();  
  186.   
  187.    
  188.             Stream streamReceive;   
  189.             string gzip = response.ContentEncoding;  
  190.   
  191.             if (string.IsNullOrEmpty(gzip) || gzip.ToLower() != "gzip")   
  192.             {   
  193.                 streamReceive = response.GetResponseStream();   
  194.             }   
  195.             else   
  196.             {   
  197.                 streamReceive = new System.IO.Compression.GZipStream(response.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress);   
  198.             }  
  199.   
  200.             sr = new System.IO.StreamReader(streamReceive, Encoding.GetEncoding(code));  
  201.   
  202.             if (response.ContentLength > 1)   
  203.             {   
  204.                 strResult = sr.ReadToEnd();   
  205.             }   
  206.             else   
  207.             {   
  208.                 char[] buffer=new char[256];   
  209.                 int count = 0;   
  210.                 StringBuilder sb = new StringBuilder();   
  211.                 while ((count = sr.Read(buffer, 0, buffer.Length)) > 0)   
  212.                 {   
  213.                     sb.Append(new string(buffer));   
  214.                 }   
  215.                 strResult = sb.ToString();   
  216.             }   
  217.             sr.Close();   
  218.             response.Close();   
  219.             return strResult;   
  220.         }  
  221.   
  222.     }   
  223. }   
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值