HttpHelper.cs
上传用户:xhd1221
上传日期:2022-05-28
资源大小:186k
文件大小:10k
源码类别:

外挂编程

开发平台:

C/C++

  1.  using System; 
  2.  using System.Collections.Generic; 
  3.  using System.Text; 
  4.  using System.Net; 
  5.  using System.IO; 
  6.  using System.Threading; 
  7. namespace QQWinFarm
  8. {
  9.    
  10.     public class HttpHelper
  11.     {
  12.         #region 变量定义
  13.         private static CookieContainer cc = new CookieContainer();
  14.         private static string contentType = "application/x-www-form-urlencoded";
  15.         private static string accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-silverlight-2-b1, */*";
  16.         private static string userAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";
  17.         private static Encoding encoding = Encoding.GetEncoding("utf-8");
  18.         private static int delay = 1000;
  19.         private static int maxTry = 300;
  20.         private static int currentTry = 0;
  21.         #endregion 
  22.         #region 变量赋值
  23.         /// <summary> 
  24.         /// Cookie
  25.         /// </summary> 
  26.         public static CookieContainer CookieContainer
  27.         {
  28.             get
  29.             {
  30.                 return cc;
  31.             }
  32.         }
  33.         /// <summary> 
  34.         /// 语言
  35.         /// </summary> 
  36.         /// <value></value> 
  37.         public static Encoding Encoding
  38.         {
  39.             get
  40.             {
  41.                 return encoding;
  42.             }
  43.             set
  44.             {
  45.                 encoding = value;
  46.             }
  47.         }
  48.         public static int NetworkDelay
  49.         {
  50.             get
  51.             {
  52.                 Random r = new Random();
  53.                 return (r.Next(delay, delay * 2));
  54.             }
  55.             set
  56.             {
  57.                 delay = value;
  58.             }
  59.         }
  60.         public static int MaxTry
  61.         {
  62.             get
  63.             {
  64.                 return maxTry;
  65.             }
  66.             set
  67.             {
  68.                 maxTry = value;
  69.             }
  70.         }
  71.         #endregion 
  72.         
  73.         #region 获取HTML
  74.         /// <summary>
  75.         /// 获取HTML
  76.         /// </summary>
  77.         /// <param name="url">地址</param>
  78.         /// <param name="postData">post 提交的字符串</param>
  79.         /// <param name="isPost">是否是post</param>
  80.         /// <param name="cookieContainer">CookieContainer</param>
  81.         /// <returns>html </returns>
  82.         public static string GetHtml(string url, string postData, bool isPost, CookieContainer cookieContainer)
  83.         {
  84.             if (string.IsNullOrEmpty(postData))
  85.             {
  86.                 return GetHtml(url, cookieContainer);
  87.             }
  88.             Thread.Sleep(NetworkDelay);//等待
  89.             currentTry++;
  90.             HttpWebRequest httpWebRequest = null;
  91.             HttpWebResponse httpWebResponse = null;
  92.             try
  93.             {
  94.                 byte[] byteRequest = Encoding.Default.GetBytes(postData);
  95.                 httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
  96.                 httpWebRequest.CookieContainer = cookieContainer;
  97.                 httpWebRequest.ContentType = contentType;
  98.                 httpWebRequest.ServicePoint.ConnectionLimit = maxTry;
  99.                 httpWebRequest.Referer = url;
  100.                 httpWebRequest.Accept = accept;
  101.                 httpWebRequest.UserAgent = userAgent;
  102.                 httpWebRequest.Method = isPost ? "POST" : "GET";
  103.                 httpWebRequest.ContentLength = byteRequest.Length;
  104.                 Stream stream = httpWebRequest.GetRequestStream();
  105.                 stream.Write(byteRequest, 0, byteRequest.Length);
  106.                 stream.Close();
  107.                 httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
  108.                 Stream responseStream = httpWebResponse.GetResponseStream();
  109.                 StreamReader streamReader = new StreamReader(responseStream, encoding);
  110.                 string html = streamReader.ReadToEnd();
  111.                 streamReader.Close();
  112.                 responseStream.Close();
  113.                 currentTry = 0;
  114.                 httpWebRequest.Abort();
  115.                 httpWebResponse.Close();
  116.                 return html;
  117.             }
  118.             catch (Exception e)
  119.             {
  120.                 //Console.ForegroundColor = ConsoleColor.Red;
  121.                 //Console.WriteLine(DateTime.Now.ToString("HH:mm:ss ") + e.Message);
  122.                 //Console.ForegroundColor = ConsoleColor.White;
  123.                 if (currentTry <= maxTry)
  124.                 {
  125.                     GetHtml(url, postData, isPost, cookieContainer);
  126.                 }
  127.                 currentTry--;
  128.                 if (httpWebRequest != null)
  129.                 {
  130.                     httpWebRequest.Abort();
  131.                 } if (httpWebResponse != null)
  132.                 {
  133.                     httpWebResponse.Close();
  134.                 }
  135.                 return string.Empty;
  136.             }
  137.         }
  138.         /// <summary>
  139.         /// 获取HTML
  140.         /// </summary>
  141.         /// <param name="url">地址</param>
  142.         /// <param name="cookieContainer">CookieContainer</param>
  143.         /// <returns>HTML</returns>
  144.         public static string GetHtml(string url, CookieContainer cookieContainer)
  145.         {
  146.             Thread.Sleep(NetworkDelay);
  147.             currentTry++;
  148.             HttpWebRequest httpWebRequest = null;
  149.             HttpWebResponse httpWebResponse = null;
  150.             try
  151.             {
  152.                 httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
  153.                 httpWebRequest.CookieContainer = cookieContainer;
  154.                 httpWebRequest.ContentType = contentType;
  155.                 httpWebRequest.ServicePoint.ConnectionLimit = maxTry;
  156.                 httpWebRequest.Referer = url;
  157.                 httpWebRequest.Accept = accept;
  158.                 httpWebRequest.UserAgent = userAgent;
  159.                 httpWebRequest.Method = "GET";
  160.                 httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
  161.                 Stream responseStream = httpWebResponse.GetResponseStream();
  162.                 StreamReader streamReader = new StreamReader(responseStream, encoding);
  163.                 string html = streamReader.ReadToEnd();
  164.                 streamReader.Close();
  165.                 responseStream.Close();
  166.                 currentTry--;
  167.                 httpWebRequest.Abort();
  168.                 httpWebResponse.Close();
  169.                 return html;
  170.             }
  171.             catch (Exception e)
  172.             {
  173.                 //Console.ForegroundColor = ConsoleColor.Red;
  174.                 //Console.WriteLine(DateTime.Now.ToString("HH:mm:ss ") + e.Message);
  175.                 //Console.ForegroundColor = ConsoleColor.White;
  176.                 if (currentTry <= maxTry)
  177.                 {
  178.                     GetHtml(url, cookieContainer);
  179.                 }
  180.                 currentTry--;
  181.                 if (httpWebRequest != null)
  182.                 {
  183.                     httpWebRequest.Abort();
  184.                 } if (httpWebResponse != null)
  185.                 {
  186.                     httpWebResponse.Close();
  187.                 }
  188.                 return string.Empty;
  189.             }
  190.         }
  191.         /// <summary>
  192.         /// 获取HTML
  193.         /// </summary>
  194.         /// <param name="url">地址</param>
  195.         /// <returns>HTML</returns>
  196.         public static string GetHtml(string url)
  197.         {
  198.             return GetHtml(url, cc);
  199.         }
  200.         /// <summary>
  201.         /// 获取HTML
  202.         /// </summary>
  203.         /// <param name="url">地址</param>
  204.         /// <param name="postData">提交的字符串</param>
  205.         /// <param name="isPost">是否是POST</param>
  206.         /// <returns>HTML</returns>
  207.         public static string GetHtml(string url, string postData, bool isPost)
  208.         {
  209.             return GetHtml(url, postData, isPost, cc);
  210.         }
  211.         /// <summary>
  212.         /// 获取字符流
  213.         /// </summary>
  214.         /// <param name="url">地址</param>
  215.         /// <param name="cookieContainer">cookieContainer</param>
  216.         /// <returns>Stream</returns>
  217.         public static Stream GetStream(string url, CookieContainer cookieContainer)
  218.         {
  219.             //Thread.Sleep(delay); 
  220.             currentTry++;
  221.             HttpWebRequest httpWebRequest = null;
  222.             HttpWebResponse httpWebResponse = null;
  223.             try
  224.             {
  225.                 httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
  226.                 httpWebRequest.CookieContainer = cookieContainer;
  227.                 httpWebRequest.ContentType = contentType;
  228.                 httpWebRequest.ServicePoint.ConnectionLimit = maxTry;
  229.                 httpWebRequest.Referer = url;
  230.                 httpWebRequest.Accept = accept;
  231.                 httpWebRequest.UserAgent = userAgent;
  232.                 httpWebRequest.Method = "GET";
  233.                 httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
  234.                 Stream responseStream = httpWebResponse.GetResponseStream();
  235.                 currentTry--;
  236.                 //httpWebRequest.Abort(); 
  237.                 //httpWebResponse.Close(); 
  238.                 return responseStream;
  239.             }
  240.             catch (Exception e)
  241.             {
  242.                 //Console.ForegroundColor = ConsoleColor.Red;
  243.                 //Console.WriteLine(DateTime.Now.ToString("HH:mm:ss ") + e.Message);
  244.                 //Console.ForegroundColor = ConsoleColor.White;
  245.                 if (currentTry <= maxTry)
  246.                 {
  247.                     GetHtml(url, cookieContainer);
  248.                 }
  249.                 currentTry--;
  250.                 if (httpWebRequest != null)
  251.                 {
  252.                     httpWebRequest.Abort();
  253.                 } if (httpWebResponse != null)
  254.                 {
  255.                     httpWebResponse.Close();
  256.                 }
  257.                 return null;
  258.             }
  259.         } 
  260.         #endregion
  261.         
  262.     }
  263. }