1. using System; 
  2. using System.Data; 
  3. using System.Configuration; 
  4. using System.Web; 
  5. using System.Web.Security; 
  6. using System.Web.UI; 
  7. using System.Web.UI.WebControls; 
  8. using System.Web.UI.WebControls.WebParts; 
  9. using System.Web.UI.HtmlControls; 
  10. using MSXML2; 
  11. using System.Text.RegularExpressions; 
  12. namespace EC 
  13. { 
  14.     /// 
  15.     /// 远程文件抓取类 
  16.     /// 
  17.     public class GetRemoteObj 
  18.     { 
  19.         #region 构造与析构函数 
  20.         public GetRemoteObj() 
  21.         { 
  22.             // 
  23.             // TODO: 在此处添加构造函数逻辑 
  24.             // 
  25.         } 
  26.         ~GetRemoteObj() 
  27.         { 
  28.             Dispose(); 
  29.         } 
  30.         #endregion 
  31.         #region IDisposable 成员 
  32.         public void Dispose() 
  33.         { 
  34.             GC.SuppressFinalize(this); 
  35.         } 
  36.         #endregion 
  37.         #region 日期随机函数 
  38.         /********************************** 
  39.          * 函数名称:DateRndName 
  40.          * 功能说明:日期随机函数 
  41.          * 参    数:ra:随机数 
  42.          * 调用示例: 
  43.          *          GetRemoteObj o = new GetRemoteObj(); 
  44.          *          Random ra = new Random(); 
  45.          *          string s = o.DateRndName(ra); 
  46.          *          Response.Write(s); 
  47.          *          o.Dispose(); 
  48.          * ********************************/ 
  49.         /// 
  50.         /// 日期随机函数 
  51.         /// 
  52.         /// 随机数 
  53.         /// 
  54.         public string DateRndName(Random ra) 
  55.         { 
  56.             DateTime d = DateTime.Now; 
  57.             string s = null, y, m, dd, h, mm, ss; 
  58.             y = d.Year.ToString(); 
  59.             m = d.Month.ToString(); 
  60.             if (m.Length < 2) m = "0" + m; 
  61.             dd = d.Day.ToString(); 
  62.             if (dd.Length < 2) dd = "0" + dd; 
  63.             h = d.Hour.ToString(); 
  64.             if (h.Length < 2) h = "0" + h; 
  65.             mm = d.Minute.ToString(); 
  66.             if (mm.Length < 2) mm = "0" + mm; 
  67.             ss = d.Second.ToString(); 
  68.             if (ss.Length < 2) ss = "0" + ss; 
  69.             s += y + m + dd + h + mm + ss; 
  70.             s += ra.Next(100, 999).ToString(); 
  71.             return s; 
  72.         } 
  73.         #endregion 
  74.         #region 取得文件后缀 
  75.         /********************************** 
  76.          * 函数名称:GetFileExtends 
  77.          * 功能说明:取得文件后缀 
  78.          * 参    数:filename:文件名称 
  79.          * 调用示例: 
  80.          *          GetRemoteObj o = new GetRemoteObj(); 
  81.          *          string url = @"http://www.baidu.com/img/logo.gif"; 
  82.          *          string s = o.GetFileExtends(url); 
  83.          *          Response.Write(s); 
  84.          *          o.Dispose(); 
  85.          * ********************************/ 
  86.         /// 
  87.         /// 取得文件后缀 
  88.         /// 
  89.         /// 文件名称 
  90.         /// 
  91.         public string GetFileExtends(string filename) 
  92.         { 
  93.             string ext = null; 
  94.             if (filename.IndexOf('.') > 0) 
  95.             { 
  96.                 string[] fs = filename.Split('.'); 
  97.                 ext = fs[fs.Length - 1]; 
  98.             } 
  99.             return ext; 
  100.         } 
  101.         #endregion 
  102.         #region 获取远程文件源代码 
  103.         /********************************** 
  104.          * 函数名称:GetRemoteHtmlCode 
  105.          * 功能说明:获取远程文件源代码 
  106.          * 参    数:Url:远程url 
  107.          * 调用示例: 
  108.          *          GetRemoteObj o = new GetRemoteObj(); 
  109.          *          string url = @"http://www.baidu.com"; 
  110.          *          string s = o.GetRemoteHtmlCode(url); 
  111.          *          Response.Write(s); 
  112.          *          o.Dispose(); 
  113.          * ********************************/ 
  114.         /// 
  115.         /// 获取远程文件源代码 
  116.         /// 
  117.         /// 远程url 
  118.         /// 
  119.         public string GetRemoteHtmlCode(string Url) 
  120.         { 
  121.             string s = ""; 
  122.             MSXML2.XMLHTTP _xmlhttp = new MSXML2.XMLHTTPClass(); 
  123.             _xmlhttp.open("GET", Url, false, null, null); 
  124.             _xmlhttp.send(""); 
  125.             if (_xmlhttp.readyState == 4) 
  126.             { 
  127.                 s = System.Text.Encoding.Default.GetString((byte[])_xmlhttp.responseBody); 
  128.             } 
  129.             return s; 
  130.         } 
  131.         #endregion 
  132.         #region 保存远程文件 
  133.         /********************************** 
  134.          * 函数名称:RemoteSave 
  135.          * 功能说明:保存远程文件 
  136.          * 参    数:Url:远程url;Path:保存到的路径 
  137.          * 调用示例: 
  138.          *          GetRemoteObj o = new GetRemoteObj(); 
  139.          *          string s = ""; 
  140.          *          string url = @"http://www.baidu.com/img/logo.gif"; 
  141.          *          string path =Server.MapPath("Html/"); 
  142.          *          s = o.RemoteSave(url,path); 
  143.          *          Response.Write(s); 
  144.          *          o.Dispose();          
  145.          * ******************************/ 
  146.         /// 
  147.         /// 保存远程文件 
  148.         /// 
  149.         /// 远程url 
  150.         /// 保存到的路径 
  151.         /// 
  152.         public string RemoteSave(string Url, string Path) 
  153.         { 
  154.             Random ra = new Random(); 
  155.             string StringFileName = DateRndName(ra) + "." + GetFileExtends(Url); 
  156.             string StringFilePath = Path + StringFileName; 
  157.             MSXML2.XMLHTTP _xmlhttp = new MSXML2.XMLHTTPClass(); 
  158.             _xmlhttp.open("GET", Url, false, null, null); 
  159.             _xmlhttp.send(""); 
  160.             if (_xmlhttp.readyState == 4) 
  161.             { 
  162.                 if (System.IO.File.Exists(StringFilePath)) 
  163.                     System.IO.File.Delete(StringFilePath); 
  164.                 System.IO.FileStream fs = new System.IO.FileStream(StringFilePath, System.IO.FileMode.CreateNew); 
  165.                 System.IO.BinaryWriter w = new System.IO.BinaryWriter(fs); 
  166.                 w.Write((byte[])_xmlhttp.responseBody); 
  167.                 w.Close(); 
  168.                 fs.Close(); 
  169.             } 
  170.             else 
  171.                 throw new Exception(_xmlhttp.statusText); 
  172.             return StringFileName; 
  173.         } 
  174.         #endregion 
  175.         #region 替换网页中的换行和引号 
  176.         /********************************** 
  177.          * 函数名称:ReplaceEnter 
  178.          * 功能说明:替换网页中的换行和引号 
  179.          * 参    数:HtmlCode:html源代码 
  180.          * 调用示例: 
  181.          *          GetRemoteObj o = new GetRemoteObj(); 
  182.          *          string Url = @"http://www.baidu.com"; 
  183.          *          strion HtmlCode = o.GetRemoteHtmlCode(Url); 
  184.          *          string s = o.ReplaceEnter(HtmlCode); 
  185.          *          Response.Write(s); 
  186.          *          o.Dispose(); 
  187.          * ********************************/ 
  188.         /// 
  189.         /// 替换网页中的换行和引号 
  190.         /// 
  191.         /// HTML源代码 
  192.         /// 
  193.         public string ReplaceEnter(string HtmlCode) 
  194.         { 
  195.             string s = ""; 
  196.             if (HtmlCode == null || HtmlCode == "") 
  197.                 s = ""; 
  198.             else 
  199.                 s = HtmlCode.Replace("/"", ""); 
  200.             s = s.Replace("/r/n", ""); 
  201.             return s; 
  202.         } 
  203.         #endregion 
  204.         #region 执行正则提取出值 
  205.         /********************************** 
  206.          * 函数名称:GetRegValue 
  207.          * 功能说明:执行正则提取出值 
  208.          * 参    数:HtmlCode:html源代码 
  209.          * 调用示例: 
  210.          *          GetRemoteObj o = new GetRemoteObj(); 
  211.          *          string Url = @"http://www.baidu.com"; 
  212.          *          strion HtmlCode = o.GetRemoteHtmlCode(Url); 
  213.          *          string s = o.ReplaceEnter(HtmlCode); 
  214.          *          string Reg=""; 
  215.          *          string GetValue=o.GetRegValue(Reg,HtmlCode) 
  216.          *          Response.Write(GetValue); 
  217.          *          o.Dispose(); 
  218.          * ********************************/ 
  219.         /// 
  220.         /// 执行正则提取出值 
  221.         /// 
  222.         /// 正则表达式 
  223.         /// HtmlCode源代码 
  224.         /// 
  225.         public string GetRegValue(string RegexString, string RemoteStr) 
  226.         { 
  227.             string MatchVale = ""; 
  228.             Regex r = new Regex(RegexString); 
  229.             Match m = r.Match(RemoteStr); 
  230.             if (m.Success) 
  231.             { 
  232.                 MatchVale = m.Value; 
  233.             } 
  234.             return MatchVale; 
  235.         } 
  236.         #endregion 
  237.         #region 替换HTML源代码 
  238.         /********************************** 
  239.          * 函数名称:RemoveHTML 
  240.          * 功能说明:替换HTML源代码 
  241.          * 参    数:HtmlCode:html源代码 
  242.          * 调用示例: 
  243.          *          GetRemoteObj o = new GetRemoteObj(); 
  244.          *          string Url = @"http://www.baidu.com"; 
  245.          *          strion HtmlCode = o.GetRemoteHtmlCode(Url); 
  246.          *          string s = o.ReplaceEnter(HtmlCode); 
  247.          *          string Reg=""; 
  248.          *          string GetValue=o.GetRegValue(Reg,HtmlCode) 
  249.          *          Response.Write(GetValue); 
  250.          *          o.Dispose(); 
  251.          * ********************************/ 
  252.         /// 
  253.         /// 替换HTML源代码 
  254.         /// 
  255.         /// html源代码 
  256.         /// 
  257.         public string RemoveHTML(string HtmlCode) 
  258.         { 
  259.             string MatchVale = HtmlCode; 
  260.             foreach (Match s in Regex.Matches(HtmlCode, "<.+?>")) 
  261.             { 
  262.                 MatchVale = MatchVale.Replace(s.Value, ""); 
  263.             } 
  264.             return MatchVale; 
  265.         } 
  266.         #endregion 
  267.         #region 匹配页面的链接 
  268.         /********************************** 
  269.          * 函数名称:GetHref 
  270.          * 功能说明:匹配页面的链接 
  271.          * 参    数:HtmlCode:html源代码 
  272.          * 调用示例: 
  273.          *          GetRemoteObj o = new GetRemoteObj(); 
  274.          *          string Url = @"http://www.baidu.com"; 
  275.          *          strion HtmlCode = o.GetRemoteHtmlCode(Url); 
  276.          *          string s = o.GetHref(HtmlCode); 
  277.          *          Response.Write(s); 
  278.          *          o.Dispose(); 
  279.          * ********************************/ 
  280.         /// 
  281.         /// 获取页面的链接正则 
  282.         /// 
  283.         /// 
  284.         /// 
  285.         public string GetHref(string HtmlCode) 
  286.         { 
  287.             string MatchVale = ""; 
  288.             string Reg = @"(h|H)(r|R)(e|E)(f|F) *= *('|"")?((/w|//|//|/.|:|-|_)+)('|""| *|>)?"; 
  289.             foreach (Match m in Regex.Matches(HtmlCode, Reg)) 
  290.             { 
  291.                 MatchVale += (m.Value).ToLower().Replace("href=", "").Trim() + "||"; 
  292.             } 
  293.             return MatchVale; 
  294.         } 
  295.         #endregion 
  296.         #region 匹配页面的图片地址 
  297.         /********************************** 
  298.          * 函数名称:GetImgSrc 
  299.          * 功能说明:匹配页面的图片地址 
  300.          * 参    数:HtmlCode:html源代码;imgHttp:要补充的http.当比如:则要补充http://www.baidu.com/,当包含http信息时,则可以为空 
  301.          * 调用示例: 
  302.          *          GetRemoteObj o = new GetRemoteObj(); 
  303.          *          string Url = @"http://www.baidu.com"; 
  304.          *          strion HtmlCode = o.GetRemoteHtmlCode(Url); 
  305.          *          string s = o.GetImgSrc(HtmlCode,"http://www.baidu.com/"); 
  306.          *          Response.Write(s); 
  307.          *          o.Dispose(); 
  308.          * ********************************/ 
  309.         /// 
  310.         /// 匹配页面的图片地址 
  311.         /// 
  312.         /// 
  313.         /// 要补充的http://路径信息 
  314.         /// 
  315.         public string GetImgSrc(string HtmlCode, string imgHttp) 
  316.         { 
  317.             string MatchVale = ""; 
  318.             string Reg = @""; 
  319.             foreach (Match m in Regex.Matches(HtmlCode, Reg)) 
  320.             { 
  321.                 MatchVale += GetImg((m.Value).ToLower().Trim(), imgHttp) + "||"; 
  322.             } 
  323.             return MatchVale; 
  324.         } 
  325.         /// 
  326.         /// 匹配中的图片路径实际链接 
  327.         /// 
  328.         /// 字符串 
  329.         /// 
  330.         public string GetImg(string ImgString, string imgHttp) 
  331.         { 
  332.             string MatchVale = ""; 
  333.             string Reg = @"src=.+/.(bmp|jpg|gif|png|)"; 
  334.             foreach (Match m in Regex.Matches(ImgString.ToLower(), Reg)) 
  335.             { 
  336.                 MatchVale += (m.Value).ToLower().Trim().Replace("src=", ""); 
  337.             } 
  338.             return (imgHttp + MatchVale); 
  339.         } 
  340.         #endregion 
  341.         #region 替换通过正则获取字符串所带的正则首尾匹配字符串 
  342.         /********************************** 
  343.          * 函数名称:GetHref 
  344.          * 功能说明:匹配页面的链接 
  345.          * 参    数:HtmlCode:html源代码 
  346.          * 调用示例: 
  347.          *          GetRemoteObj o = new GetRemoteObj(); 
  348.          *          string Url = @"http://www.baidu.com"; 
  349.          *          strion HtmlCode = o.GetRemoteHtmlCode(Url); 
  350.          *          string s = o.RegReplace(HtmlCode,""); 
  351.          *          Response.Write(s); 
  352.          *          o.Dispose(); 
  353.          * ********************************/ 
  354.         /// 
  355.         /// 替换通过正则获取字符串所带的正则首尾匹配字符串 
  356.         /// 
  357.         /// 要替换的值 
  358.         /// 正则匹配的首字符串 
  359.         /// 正则匹配的尾字符串 
  360.         /// 
  361.         public string RegReplace(string RegValue, string regStart, string regEnd) 
  362.         { 
  363.             string s = RegValue; 
  364.             if (RegValue != "" && RegValue != null) 
  365.             { 
  366.                 if (regStart != "" && regStart != null) 
  367.                 { 
  368.                     s = s.Replace(regStart, ""); 
  369.                 } 
  370.                 if (regEnd != "" && regEnd != null) 
  371.                 { 
  372.                     s = s.Replace(regEnd, ""); 
  373.                 } 
  374.             } 
  375.             return s; 
  376.         } 
  377.         #endregion 
  378.     } 
  379. } 
  380. 记得添加引用 Interop.MSXML2.dll 否则msxml2命名空间用不了

本文转载:CSDN博客