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. CookieCollection ckclReturn = new CookieCollection();
  92. CookieContainer cc = new CookieContainer();
  93. HttpWebRequest hwRequest;
  94. HttpWebResponse hwResponse;
  95. //请求cookies的格式
  96. //hwRequest = (HttpWebRequest)HttpWebRequest.Create(new Uri(url));
  97. //hwResponse = (HttpWebResponse)hwRequest.GetResponse();
  98. //string cookie = hwResponse.Headers.Get("Set-Cookie");
  99. //cookie = cookie.Split(';')[0];
  100. //hwRequest = null;
  101. //hwResponse = null;
  102. //构建即将发送的包头
  103. //cc.SetCookies(new Uri(server), cookie);
  104. hwRequest = (HttpWebRequest)HttpWebRequest.Create(new Uri(url));
  105. hwRequest.CookieContainer = cc;
  106. hwRequest.Accept = rppt.Accept;
  107. hwRequest.ContentType = rppt.ContentType;
  108. hwRequest.UserAgent = rppt.UserAgent;
  109. hwRequest.Method = rppt.Method;
  110. hwRequest.ContentLength = post.Length;
  111. //写入标头
  112. Stream stream;
  113. stream = hwRequest.GetRequestStream();
  114. stream.Write(post, 0, post.Length);
  115. stream.Close();
  116. //发送请求获取响应内容
  117. try
  118. {
  119. hwResponse = (HttpWebResponse)hwRequest.GetResponse();
  120. }
  121. catch
  122. {
  123. strHtml = "";
  124. return ckclReturn;
  125. }
  126. stream = hwResponse.GetResponseStream();
  127. StreamReader sReader = new StreamReader(stream, Encoding.Default);
  128. strHtml = sReader.ReadToEnd();
  129. sReader.Close();
  130. stream.Close();
  131. //获取缓存内容
  132. ckclReturn = hwResponse.Cookies;
  133. return ckclReturn;
  134. }
  135. /// <summary>
  136. /// 根据已经获取的有效cookies来获取目标链接的内容
  137. /// </summary>
  138. /// <param name="strUri">目标链接的url
  139. /// <param name="ccl">已经获取到的有效cookies
  140. /// <param name="rppt">头属性的相关设置
  141. /// <returns>目标连接的纯文本:"txt/html"</returns>
  142. public string funGetHtmlByCookies(string strUri, CookieCollection ccl, RequestPPT rppt)
  143. {
  144. CookieContainer cc = new CookieContainer();
  145. HttpWebRequest hwRequest;
  146. HttpWebResponse hwResponse;
  147. //构建即将发送的包头
  148. hwRequest = (HttpWebRequest)HttpWebRequest.Create(new Uri(strUri));
  149. cc.Add(ccl);
  150. hwRequest.CookieContainer = cc;
  151. hwRequest.Accept = rppt.Accept;
  152. hwRequest.ContentType = rppt.ContentType;
  153. hwRequest.UserAgent = rppt.UserAgent;
  154. hwRequest.Method = rppt.Method;
  155. hwRequest.ContentLength = 0;
  156. //发送请求获取响应内容
  157. try
  158. {
  159. hwResponse = (HttpWebResponse)hwRequest.GetResponse();
  160. }
  161. catch
  162. {
  163. return "";
  164. }
  165. Stream stream;
  166. stream = hwResponse.GetResponseStream();
  167. StreamReader sReader = new StreamReader(stream, Encoding.Default);
  168. string strHtml = sReader.ReadToEnd();
  169. sReader.Close();
  170. stream.Close();
  171. //返回值
  172. return strHtml;
  173. }
  174. /// <summary>
  175. /// 根据已经获取的有效cookies来获取目标链接的内容
  176. /// </summary>
  177. /// <param name="strUri">目标链接的url
  178. ///<param name="post">post的byte信息
  179. /// <param name="ccl">已经获取到的有效cookies
  180. /// <param name="rppt">头属性的相关设置
  181. /// <returns>目标连接的纯文本:"txt/html"</returns>
  182. public string funGetHtmlByCookies(string strUri,byte[] post, CookieCollection ccl, RequestPPT rppt)
  183. {
  184. CookieContainer cc = new CookieContainer();
  185. HttpWebRequest hwRequest;
  186. HttpWebResponse hwResponse;
  187. //构建即将发送的包头
  188. hwRequest = (HttpWebRequest)HttpWebRequest.Create(new Uri(strUri));
  189. cc.Add(ccl);
  190. hwRequest.CookieContainer = cc;
  191. hwRequest.Accept = rppt.Accept;
  192. hwRequest.ContentType = rppt.ContentType;
  193. hwRequest.UserAgent = rppt.UserAgent;
  194. hwRequest.Method = rppt.Method;
  195. hwRequest.ContentLength = post.Length;
  196. //写入post信息
  197. Stream stream;
  198. stream = hwRequest.GetRequestStream();
  199. stream.Write(post, 0, post.Length);
  200. stream.Close();
  201. //发送请求获取响应内容
  202. try
  203. {
  204. hwResponse = (HttpWebResponse)hwRequest.GetResponse();
  205. }
  206. catch
  207. {
  208. return"" ;
  209. }
  210. stream = hwResponse.GetResponseStream();
  211. StreamReader sReader = new StreamReader(stream, Encoding.Default);
  212. string strHtml = sReader.ReadToEnd();
  213. sReader.Close();
  214. stream.Close();
  215. //返回值
  216. return strHtml;
  217. }
  218. }
  219. }
  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. /// <summary>
    17. /// 设置主机ip地址
    18. /// </summary>
    19. public string Host
    20. {
    21. get {
    22. return strHost;
    23. }
    24. set {
    25. strHost = value;
    26. }
    27. }
    28. private string strHost;
    29. /// <summary>
    30. /// 获取目标登录链接的cookies
    31. /// </summary>
    32. /// <param name="url">目标的登录链接</param>
    33. /// <param name="dir">构造头的泛型键值对</param>
    34. /// <param name="strHtml">登录后返回的页面内容</param>
    35. /// <returns>登录后的cookies</returns>
    36. public CookieCollection funGetCookie(string url, Dictionary<string, string> dir, out string strHtml)
    37. {
    38. CookieCollection cc = new CookieCollection();
    39. RequestPPT rppt = new RequestPPT();
    40. //构建post内容
    41. string strPost = funMakePost(dir);
    42. byte[] post = Encoding.Default.GetBytes(strPost);
    43. //设置标头属性
    44. 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, */*";
    45. rppt.ContentType = "application/x-www-form-urlencoded";
    46. rppt.Method = "Post";
    47. 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)";
    48. string server ="http://"+ new Uri(url).Host;
    49. return cc = base.funGetCookie(url, post, out strHtml, rppt, server);
    50. }
    51. /// <summary>
    52. /// 根据已经获取到cookies来获取目标链接的内容
    53. /// </summary>
    54. /// <param name="strUri">目标的url</param>
    55. /// <param name="ccl">已经获取好的cookies</param>
    56. /// <returns>目标url的纯文本:"txt/html"</returns>
    57. public string funGetHtmlByCookies(string strUri,CookieCollection ccl )
    58. {
    59. RequestPPT rppt = new RequestPPT();
    60. //设置头属性
    61. rppt.Accept = "txt/html";
    62. rppt.ContentType = "application/x-www-form-urlencoded";
    63. rppt.Method = "Post";
    64. 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)";
    65. return base.funGetHtmlByCookies(strUri, ccl, rppt);
    66. }
    67. /// <summary>
    68. /// 投票帖子用的方法
    69. /// </summary>
    70. /// <param name="strHtml">投票帖子的htmlcode</param>
    71. /// <param name="ccl">有效的cookies</param>
    72. /// <returns>投票完成以后的htmlcode</returns>
    73. public string  funVote(string strHtml,CookieCollection ccl)
    74. {
    75. //判断是不是选取投票
    76. try
    77. {
    78. strHtml = strHtml.Substring(strHtml.IndexOf("<form"), strHtml.LastIndexOf("</form>") - strHtml.IndexOf("<form") + 7);
    79. }
    80. catch
    81. {
    82. return "";
    83. }
    84. string strCheck = @"name=""pollanswers[]""";
    85. //如果代码中包含关键信息说明没有被投票过
    86. if(strHtml.IndexOf(strCheck)>0)
    87. {
    88. //获取post头的需求信息
    89. string strFormHash = "77b49df4";
    90. string strPollanswers;
    91. strPollanswers = strHtml.Substring(strHtml.IndexOf(strCheck)+strCheck.Length, 20).Split('"')[1];
    92. string strPollansubmit = "提交";
    93. Dictionary<string,string>dir = new Dictionary<string,string>();
    94. dir.Add("formhash",strFormHash);
    95. dir.Add("pollanswers[]",strPollanswers);
    96. dir.Add("pollsubmit",strPollansubmit);
    97. string strPost = funMakePost(dir);
    98. byte[] post = Encoding.Default.GetBytes(strPost);
    99. //获取请求的路径
    100. string strUrl= "http://"+Host+"/bbs/";
    101. string strActionUrl =@"method=""post""";
    102. strUrl+= strHtml.Substring(strHtml.IndexOf(strActionUrl)+strActionUrl.Length,100).Split('"')[1].Replace("amp;","");
    103. //构建头
    104. RequestPPT rppt = new RequestPPT();
    105. rppt.Accept = "txt/html";
    106. rppt.ContentType = "application/x-www-form-urlencoded";
    107. rppt.Method = "Post";
    108. 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)";
    109. strHtml = base.funGetHtmlByCookies(strUrl, post, ccl, rppt);
    110. }
    111. return strHtml;
    112. }
    113. /// <summary>
    114. /// 根据泛型来构建字符串用于post
    115. /// </summary>
    116. /// <param name="dir">带有键值对的泛型</param>
    117. /// <returns>构建完毕的字符串</returns>
    118. private string funMakePost(Dictionary<string,string> dir)
    119. {
    120. string strPost="";
    121. foreach (KeyValuePair<string, string> kvp in dir)
    122. {
    123. strPost += kvp.Key + "=";
    124. if (kvp.Value == "")
    125. {
    126. strPost += "''";
    127. }
    128. else
    129. {
    130. strPost += kvp.Value;
    131. }
    132. strPost += "&";
    133. }
    134. strPost = strPost.Substring(0, strPost.Length - 1);
    135. return strPost;
    136. }
    137. /// <summary>
    138. /// 获取下一个列表页面的路径
    139. /// </summary>
    140. /// <param name="strHtml">当前页面的htmlcode</param>
    141. /// <returns>下一个列表页面的路径</returns>
    142. public string funGetNextUrl(string strHtml)
    143. {
    144. string strUrl = "";
    145. //判断是否是列表型页面
    146. if (strHtml.IndexOf("<form") != -1)
    147. {
    148. return strUrl;
    149. }
    150. string strKey =@"class=""next""";
    151. strUrl = "http://"+Host+"/bbs/"+strHtml.Substring(strHtml.IndexOf(strKey) - 100, 100).Split('"')[1].Replace("amp;", "");
    152. return strUrl;
    153. }
    154. public DataTable funGetListTable(string strHtml)
    155. {
    156. DataTable dt = new DataTable();
    157. DataColumn dc = new DataColumn("Url");
    158. dt.Columns.Add(dc);
    159. DataRow dr ;
    160. string strReg = @"viewthread.php(/S)+highlight=";
    161. Regex rg = new Regex(strReg);
    162. MatchCollection mc = rg.Matches(strHtml);
    163. foreach (Match ms in mc)
    164. {
    165. dr = dt.NewRow();
    166. dr[0] = "http://" + Host + "/bbs/" + ms.ToString().Replace("amp;", "");
    167. dt.Rows.Add(dr);
    168. }
    169. return dt;
    170. }
    171. }
    172. }
    173. </pre>

httpwebrequest 模拟登录 获取cookies 以前的代码,记录备忘!的更多相关文章

  1. 记一次HTTPClient模拟登录获取Cookie的开发历程

    记一次HTTPClient模拟登录获取Cookie的开发历程 环境: ​ springboot : 2.7 ​ jdk: 1.8 ​ httpClient : 4.5.13 设计方案 ​ 通过新建一个 ...

  2. HttpWebRequest 模拟登录响应点击事件(分享自己用的HttpHelper类)

    平时也经常采集网站数据,也做模拟登录,但一般都是html控件POST到页面登录:还没有遇到用户服务器控件button按钮点击事件登录的,今天像往常一样POST传递参数,但怎么都能登录不了:最后发现还有 ...

  3. POST信息模拟登录获取页面内容

    最近项目里有一个是要模拟登录后,访问固定页面获取内容的要求,一开始用JQ AJAX好像不支持跨域请求.后使用.net中HttpWebRequest对象来获取.一开始访问总是无法在第二个页面正常访问,好 ...

  4. ph模拟登录获取信息

    cURL 是一个功能强大的PHP库,使用PHP的cURL库可以简单和有效地抓取网页并采集内容,设置cookie完成模拟登录网页,curl提供了丰富的函数,开发者可以从PHP手册中获取更多关于cURL信 ...

  5. 微博验证码的识别并登录获取cookies

    记得以前微博是用的宫格验证码,现在不一样了,用的是滑块验证码和 点触验证码,每天登陆的第一次基本用的是滑块,继续登录就都用的是点触验证码.所以滑块验证码不写,感兴趣的可以补上. 代码: 这里用的超级鹰 ...

  6. Python入门小练习 003 利用cookielib模拟登录获取账户信息

    为了方便, 使用chinaunix的账户获取账户主题. 有些网站可能需要验证码,  找一些不用验证码的网站 下面 ****** 很多个星号的均为私密信息, 所以用星号代替 #!/usr/bin/pyt ...

  7. C#常用代码片段备忘

    以下是从visual studio中整理出来的常用代码片段,以作备忘 快捷键: eh 用途: 类中事件实现函数模板 private void MyMethod(object sender, Event ...

  8. Python爬虫 —— 知乎之selenium模拟登陆获取cookies+requests.Session()访问+session序列化

    代码如下: # coding:utf-8 from selenium import webdriver import requests import sys import time from lxml ...

  9. 终极利器!利用appium和mitmproxy登录获取cookies

    环境搭建 参考我之前写的https://www.cnblogs.com/c-x-a/p/9163221.html appium 代码start_appium.py # -*- coding: utf- ...

随机推荐

  1. php 关了浏览器也可以自动运行脚本

    <?php ignore_user_abort(); //即使Client断开(如关掉浏览器),PHP脚本也可以继续执行. set_time_limit(0); //执行时间为无限制,php默认 ...

  2. 终于下决心在cnblogs上安家了,^_^

    以前在这个地方学到了很多东西,希望在这里安家以后,自己也可以有很多成长. mark一下,~~

  3. 修改IP的方法(C#)

    1. wmi 代码以后补 需要获取全部IP后,统一添加(貌似会造成网络瞬断) 2. iphlpapi.lib 代码以后补 可以直接添加和删除IP 3. netsh 可以直接添加和删除IP

  4. MFC中MessageBeep与sndPlaySound播放声音函数使用

    MessageBeep(0x00000000L);    //用来播放系统默认音频文件,如0x00000000L为系统提示音,具体音频对应规则,请参照MSDN. sndPlaySound函数用来播放指 ...

  5. leetcode342合理运用位操作判断4的幂

    Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example: Gi ...

  6. Linux入门(一)常见虚拟机及Linux系统安装、xshell连接虚拟机

    1环境 linux常用两种虚拟机 1.1  oracle VM VirtualBox 官方网站:https://www.virtualbox.org/ 1.2  vmware  下载链接:https: ...

  7. hdu_2665_Kth number(主席树)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2665 题意:给你一个区间,让你找这个区间第K大的数 题解:主席树模版题,也可以用划分树 #includ ...

  8. linux下shell命令trap

    某些时候,在执行shell脚本(.sh)时,我们并不希望被打断.这时我们要用到trap命令. 例如: 在shell脚本中,忽略“终止”信号 trap  ' '   TERM

  9. servlet第2讲(上集)----创建servlet实例(实现servlet接口)

  10. text-size-adjust的值为100% 代替值 none

    iPhone 横屏默认会放大文字,设置text-size-adjust会解决这个问题 一般用text-size-adjust:none 但建议用100%代替none text-size-adjust: ...