zoukankan      html  css  js  c++  java
  • httpwebrequest 模拟登录 获取cookies 以前的代码,记录备忘!

    2个类,一个基类,一个构建头信息调用类

    关于如何获取到post中的内容,你之需要用http抓包工具把你与目标网站的请求信息抓下来后,打开分析下按照抓下来的包中的数

    据进行构建就行了

    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Text;  
    4. using System.Net;  
    5. using System.IO;  
    6. namespace bot  
    7. {  
    8.    public class Html  
    9.     {  
    10.         /// <summary>  
    11.         /// httpwebrequest类中的一些属性的集合  
    12.         /// </summary>  
    13.         public struct RequestPPT  
    14.         {  
    15.             private string strAccept;  
    16.             /// <summary>  
    17.             /// 获取或设置request类中的Accept属性  
    18.             /// 用以设置接受的文件类型  
    19.             /// </summary>              
    20.             public string Accept  
    21.             {  
    22.                 get  
    23.                 {  
    24.                     return strAccept;  
    25.                 }  
    26.                 set  
    27.                 {  
    28.                     strAccept = value;  
    29.                 }  
    30.             }  
    31.             private string strContentType;  
    32.             /// <summary>  
    33.             /// 获取或设置request类中的ContentType属性  
    34.             /// 用以设置请求的媒体类型  
    35.             /// </summary>            
    36.             public string ContentType  
    37.             {  
    38.                 get  
    39.                 {  
    40.                     return strContentType;  
    41.                 }  
    42.                 set  
    43.                 {  
    44.                     strContentType = value;  
    45.                 }  
    46.             }  
    47.             /// <summary>  
    48.             /// 获取或设置request类中的UserAgent属性  
    49.             /// 用以设置请求的客户端信息  
    50.             /// </summary>  
    51.             private string strUserAgent;  
    52.             public string UserAgent  
    53.             {  
    54.                 get  
    55.                 {  
    56.                     return strUserAgent;  
    57.                 }  
    58.                 set  
    59.                 {  
    60.                     strUserAgent = value;  
    61.                 }  
    62.             }  
    63.             private string strMethod;  
    64.             /// <summary>  
    65.             /// 获取或设置request类中的Method属性  
    66.             /// 可以将 Method 属性设置为任何 HTTP 1.1 协议谓词:GET、HEAD、POST、PUT、DELETE、TRACE 或 OPTIONS。  
    67.             /// 如果 ContentLength 属性被设置为 -1 以外的任何值,则必须将 Method 属性设置为上载数据的协议属性。  
    68.             /// </summary>              
    69.             public string Method  
    70.             {  
    71.                 get  
    72.                 {  
    73.                     return strMethod;  
    74.                 }  
    75.                 set  
    76.                 {  
    77.                     strMethod = value;  
    78.                 }  
    79.             }  
    80.         }  
    81.         /// <summary>  
    82.         /// 构建一个httt请求以获取目标链接的cookies,需要传入目标的登录地址和相关的post信息,返回完成登录的cookies,以及返回的html内容  
    83.         /// </summary>  
    84.         /// <param name="url">登录页面的地址  
    85.         /// <param name="post">post信息  
    86.         /// <param name="strHtml">输出的html代码  
    87.         /// <param name="rppt">请求的标头所需要的相关属性设置  
    88.         /// <returns>请求完成后的cookies</returns>  
    89.         public CookieCollection funGetCookie(string url, byte[] post, out string strHtml, RequestPPT rppt,string server)  
    90.         {  
    91.               
    92.             CookieCollection ckclReturn = new CookieCollection();  
    93.             CookieContainer cc = new CookieContainer();  
    94.             HttpWebRequest hwRequest;  
    95.             HttpWebResponse hwResponse;  
    96.             //请求cookies的格式  
    97.             //hwRequest = (HttpWebRequest)HttpWebRequest.Create(new Uri(url));  
    98.             //hwResponse = (HttpWebResponse)hwRequest.GetResponse();  
    99.             //string cookie = hwResponse.Headers.Get("Set-Cookie");  
    100.             //cookie = cookie.Split(';')[0];  
    101.             //hwRequest = null;  
    102.             //hwResponse = null;  
    103.             //构建即将发送的包头  
    104.             //cc.SetCookies(new Uri(server), cookie);             
    105.             hwRequest = (HttpWebRequest)HttpWebRequest.Create(new Uri(url));  
    106.             hwRequest.CookieContainer = cc;  
    107.             hwRequest.Accept = rppt.Accept;  
    108.             hwRequest.ContentType = rppt.ContentType;  
    109.             hwRequest.UserAgent = rppt.UserAgent;  
    110.             hwRequest.Method = rppt.Method;  
    111.             hwRequest.ContentLength = post.Length;  
    112.             //写入标头  
    113.             Stream stream;  
    114.             stream = hwRequest.GetRequestStream();  
    115.             stream.Write(post, 0, post.Length);  
    116.             stream.Close();  
    117.             //发送请求获取响应内容  
    118.             try  
    119.             {  
    120.                 hwResponse = (HttpWebResponse)hwRequest.GetResponse();  
    121.             }  
    122.             catch  
    123.             {  
    124.                 strHtml = "";  
    125.                 return ckclReturn;  
    126.             }  
    127.             stream = hwResponse.GetResponseStream();  
    128.             StreamReader sReader = new StreamReader(stream, Encoding.Default);  
    129.             strHtml = sReader.ReadToEnd();  
    130.             sReader.Close();  
    131.             stream.Close();  
    132.             //获取缓存内容  
    133.             ckclReturn = hwResponse.Cookies;  
    134.             return ckclReturn;  
    135.         }  
    136.        /// <summary>  
    137.        /// 根据已经获取的有效cookies来获取目标链接的内容  
    138.        /// </summary>  
    139.        /// <param name="strUri">目标链接的url  
    140.        /// <param name="ccl">已经获取到的有效cookies  
    141.        /// <param name="rppt">头属性的相关设置  
    142.        /// <returns>目标连接的纯文本:"txt/html"</returns>  
    143.        public string funGetHtmlByCookies(string strUri, CookieCollection ccl, RequestPPT rppt)  
    144.        {  
    145.            CookieContainer cc = new CookieContainer();  
    146.            HttpWebRequest hwRequest;  
    147.            HttpWebResponse hwResponse;        
    148.      
    149.            //构建即将发送的包头         
    150.            hwRequest = (HttpWebRequest)HttpWebRequest.Create(new Uri(strUri));  
    151.            cc.Add(ccl);  
    152.            hwRequest.CookieContainer = cc;  
    153.            hwRequest.Accept = rppt.Accept;  
    154.            hwRequest.ContentType = rppt.ContentType;  
    155.            hwRequest.UserAgent = rppt.UserAgent;  
    156.            hwRequest.Method = rppt.Method;  
    157.            hwRequest.ContentLength = 0;      
    158.       
    159.            //发送请求获取响应内容  
    160.            try  
    161.            {  
    162.                hwResponse = (HttpWebResponse)hwRequest.GetResponse();  
    163.            }  
    164.            catch  
    165.            {  
    166.                return "";  
    167.            }  
    168.   
    169.            Stream stream;  
    170.            stream = hwResponse.GetResponseStream();  
    171.            StreamReader sReader = new StreamReader(stream, Encoding.Default);  
    172.            string strHtml = sReader.ReadToEnd();  
    173.            sReader.Close();  
    174.            stream.Close();  
    175.   
    176.            //返回值            
    177.            return strHtml;  
    178.        }  
    179.        /// <summary>  
    180.        /// 根据已经获取的有效cookies来获取目标链接的内容  
    181.        /// </summary>  
    182.        /// <param name="strUri">目标链接的url  
    183.        ///<param name="post">post的byte信息  
    184.        /// <param name="ccl">已经获取到的有效cookies  
    185.        /// <param name="rppt">头属性的相关设置  
    186.        /// <returns>目标连接的纯文本:"txt/html"</returns>  
    187.        public string funGetHtmlByCookies(string strUri,byte[] post, CookieCollection ccl, RequestPPT rppt)  
    188.        {  
    189.            CookieContainer cc = new CookieContainer();  
    190.            HttpWebRequest hwRequest;  
    191.            HttpWebResponse hwResponse;  
    192.   
    193.            //构建即将发送的包头         
    194.            hwRequest = (HttpWebRequest)HttpWebRequest.Create(new Uri(strUri));  
    195.            cc.Add(ccl);  
    196.            hwRequest.CookieContainer = cc;  
    197.            hwRequest.Accept = rppt.Accept;  
    198.            hwRequest.ContentType = rppt.ContentType;  
    199.            hwRequest.UserAgent = rppt.UserAgent;  
    200.            hwRequest.Method = rppt.Method;  
    201.            hwRequest.ContentLength = post.Length;  
    202.            //写入post信息  
    203.            Stream stream;  
    204.            stream = hwRequest.GetRequestStream();  
    205.            stream.Write(post, 0, post.Length);  
    206.            stream.Close();  
    207.            //发送请求获取响应内容  
    208.            try  
    209.            {  
    210.                hwResponse = (HttpWebResponse)hwRequest.GetResponse();  
    211.            }  
    212.            catch  
    213.            {  
    214.                return"" ;  
    215.            }  
    216.              
    217.            stream = hwResponse.GetResponseStream();  
    218.            StreamReader sReader = new StreamReader(stream, Encoding.Default);  
    219.            string strHtml = sReader.ReadToEnd();  
    220.            sReader.Close();  
    221.            stream.Close();  
    222.   
    223.            //返回值            
    224.            return strHtml;  
    225.        }  
    226.     }  
    227. }  
    1.    
    1.    
    1. 第二个  
    1.    
    1.    
      1. <pre class="">using System;  
      2. using System.IO;  
      3. using System.Collections.Generic;  
      4. using System.Text;  
      5. using System.Net;  
      6. using System.Data;  
      7. using System.Xml;  
      8. using System.Text.RegularExpressions;  
      9. namespace bot  
      10. {  
      11.     public class SisHtml :Html  
      12.     {  
      13.         public SisHtml()  
      14.         {  
      15.    
      16.         }  
      17.   
      18.         /// <summary>  
      19.         /// 设置主机ip地址  
      20.         /// </summary>  
      21.         public string Host  
      22.         {  
      23.             get {  
      24.                 return strHost;  
      25.   
      26.             }  
      27.             set {  
      28.                 strHost = value;  
      29.             }  
      30.         }  
      31.   
      32.         private string strHost;  
      33.   
      34.         /// <summary>  
      35.         /// 获取目标登录链接的cookies  
      36.         /// </summary>  
      37.         /// <param name="url">目标的登录链接</param>  
      38.         /// <param name="dir">构造头的泛型键值对</param>  
      39.         /// <param name="strHtml">登录后返回的页面内容</param>  
      40.         /// <returns>登录后的cookies</returns>  
      41.         public CookieCollection funGetCookie(string url, Dictionary<stringstring> dir, out string strHtml)  
      42.         {  
      43.             CookieCollection cc = new CookieCollection();  
      44.             RequestPPT rppt = new RequestPPT();  
      45.   
      46.             //构建post内容  
      47.             string strPost = funMakePost(dir);  
      48.             byte[] post = Encoding.Default.GetBytes(strPost);  
      49.   
      50.             //设置标头属性  
      51.             rppt.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";  
      52.             rppt.ContentType = "application/x-www-form-urlencoded";  
      53.             rppt.Method = "Post";  
      54.             rppt.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; InfoPath.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";  
      55.             string server ="http://"new Uri(url).Host;  
      56.             return cc = base.funGetCookie(url, post, out strHtml, rppt, server);  
      57.         }  
      58.   
      59.         /// <summary>  
      60.         /// 根据已经获取到cookies来获取目标链接的内容  
      61.         /// </summary>  
      62.         /// <param name="strUri">目标的url</param>  
      63.         /// <param name="ccl">已经获取好的cookies</param>  
      64.         /// <returns>目标url的纯文本:"txt/html"</returns>  
      65.         public string funGetHtmlByCookies(string strUri,CookieCollection ccl )  
      66.         {  
      67.               
      68.             RequestPPT rppt = new RequestPPT();       
      69.             //设置头属性  
      70.             rppt.Accept = "txt/html";  
      71.             rppt.ContentType = "application/x-www-form-urlencoded";  
      72.             rppt.Method = "Post";  
      73.             rppt.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; InfoPath.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";  
      74.             return base.funGetHtmlByCookies(strUri, ccl, rppt);  
      75.         }  
      76.   
      77.         
      78.         /// <summary>  
      79.         /// 投票帖子用的方法  
      80.         /// </summary>  
      81.         /// <param name="strHtml">投票帖子的htmlcode</param>  
      82.         /// <param name="ccl">有效的cookies</param>  
      83.         /// <returns>投票完成以后的htmlcode</returns>  
      84.         public string  funVote(string strHtml,CookieCollection ccl)  
      85.         {  
      86.            //判断是不是选取投票  
      87.             try  
      88.             {  
      89.                 strHtml = strHtml.Substring(strHtml.IndexOf("<form"), strHtml.LastIndexOf("</form>") - strHtml.IndexOf("<form") + 7);  
      90.             }  
      91.             catch  
      92.             {  
      93.                 return "";  
      94.             }  
      95.             string strCheck = @"name=""pollanswers[]""";  
      96.             //如果代码中包含关键信息说明没有被投票过  
      97.             if(strHtml.IndexOf(strCheck)>0)  
      98.             {  
      99.                 //获取post头的需求信息  
      100.                 string strFormHash = "77b49df4";  
      101.                 string strPollanswers;  
      102.                 strPollanswers = strHtml.Substring(strHtml.IndexOf(strCheck)+strCheck.Length, 20).Split('"')[1];  
      103.                 string strPollansubmit = "提交";  
      104.                 Dictionary<string,string>dir = new Dictionary<string,string>();  
      105.                 dir.Add("formhash",strFormHash);  
      106.                 dir.Add("pollanswers[]",strPollanswers);  
      107.                 dir.Add("pollsubmit",strPollansubmit);  
      108.                 string strPost = funMakePost(dir);  
      109.                 byte[] post = Encoding.Default.GetBytes(strPost);  
      110.                 //获取请求的路径                 
      111.                 string strUrl= "http://"+Host+"/bbs/";  
      112.                 string strActionUrl =@"method=""post""";  
      113.                 strUrl+= strHtml.Substring(strHtml.IndexOf(strActionUrl)+strActionUrl.Length,100).Split('"')[1].Replace("amp;","");  
      114.                  //构建头  
      115.                 RequestPPT rppt = new RequestPPT();  
      116.                 rppt.Accept = "txt/html";  
      117.                 rppt.ContentType = "application/x-www-form-urlencoded";  
      118.                 rppt.Method = "Post";  
      119.                 rppt.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; InfoPath.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";  
      120.                 strHtml = base.funGetHtmlByCookies(strUrl, post, ccl, rppt);  
      121.             }  
      122.             return strHtml;  
      123.         }  
      124.   
      125.         /// <summary>  
      126.         /// 根据泛型来构建字符串用于post  
      127.         /// </summary>  
      128.         /// <param name="dir">带有键值对的泛型</param>  
      129.         /// <returns>构建完毕的字符串</returns>  
      130.         private string funMakePost(Dictionary<string,string> dir)  
      131.         {  
      132.             string strPost="";  
      133.             foreach (KeyValuePair<stringstring> kvp in dir)  
      134.             {  
      135.                 strPost += kvp.Key + "=";  
      136.                 if (kvp.Value == "")  
      137.                 {  
      138.                     strPost += "''";  
      139.                 }  
      140.                 else  
      141.                 {  
      142.                     strPost += kvp.Value;  
      143.                 }  
      144.                 strPost += "&";  
      145.             }  
      146.             strPost = strPost.Substring(0, strPost.Length - 1);  
      147.             return strPost;  
      148.         }  
      149.   
      150.         /// <summary>  
      151.         /// 获取下一个列表页面的路径  
      152.         /// </summary>  
      153.         /// <param name="strHtml">当前页面的htmlcode</param>  
      154.         /// <returns>下一个列表页面的路径</returns>  
      155.         public string funGetNextUrl(string strHtml)  
      156.         {  
      157.             string strUrl = "";  
      158.             //判断是否是列表型页面  
      159.             if (strHtml.IndexOf("<form") != -1)  
      160.             {  
      161.                 return strUrl;  
      162.             }  
      163.             string strKey =@"class=""next""";  
      164.             strUrl = "http://"+Host+"/bbs/"+strHtml.Substring(strHtml.IndexOf(strKey) - 100, 100).Split('"')[1].Replace("amp;", "");  
      165.             return strUrl;  
      166.         }  
      167.   
      168.         public DataTable funGetListTable(string strHtml)  
      169.         {  
      170.             DataTable dt = new DataTable();  
      171.             DataColumn dc = new DataColumn("Url");  
      172.             dt.Columns.Add(dc);  
      173.             DataRow dr ;  
      174.             string strReg = @"viewthread.php(/S)+highlight=";  
      175.             Regex rg = new Regex(strReg);  
      176.             MatchCollection mc = rg.Matches(strHtml);  
      177.   
      178.             foreach (Match ms in mc)  
      179.             {  
      180.                 dr = dt.NewRow();  
      181.                 dr[0] = "http://" + Host + "/bbs/" + ms.ToString().Replace("amp;""");  
      182.                 dt.Rows.Add(dr);  
      183.             }  
      184.               
      185.             return dt;  
      186.         }  
      187.     }  
      188. }  
      189. </pre> 
  • 相关阅读:
    【转】130 个相见恨晚的超实用网站,一次性分享出来
    基于maven testng extentreport git jenkins 持续自动化测试
    TestNG与ExtentReport集成
    Jenkins集成jSonarQube
    SonarQube & Sonar Scanner
    Jenkins集成jacoco收集单元测试覆盖率
    Jenkins集成jacoco收集集成测试覆盖率
    Jacoco收集单元测试、集成测试和系统功能测试覆盖率
    SoanrQube7.4安装(Window)
    SoanrQube使用maven进行代码分析
  • 原文地址:https://www.cnblogs.com/xuxiaoshuan/p/3628941.html
Copyright © 2011-2022 走看看