Asp.net采集用到的幾個方法
#region 日期隨機函數
??????? /**********************************
???????? * 函數名稱:DateRndName
???????? * 功能說明:日期隨機函數
???????? * 參??? 數:ra:隨機數
???????? * 調用示例:
???????? *????????? GetRemoteObj o = new GetRemoteObj();
???????? *????????? Random ra = new Random();
???????? *????????? string s = o.DateRndName(ra);
???????? *????????? Response.Write(s);
???????? *????????? o.Dispose();
???????? * ********************************/
??????? /// <summary>
??????? /// 日期隨機函數
??????? /// </summary>
??????? /// <param name="ra">隨機數</param>
??????? /// <returns></returns>
??????? public? string DateRndName(Random ra)
??????? {
??????????? DateTime d = DateTime.Now;
??????????? string s = null, y, m, dd, h, mm, ss;
??????????? y = d.Year.ToString();
??????????? m = d.Month.ToString();
??????????? if (m.Length < 2) m = "0" + m;
??????????? dd = d.Day.ToString();
??????????? if (dd.Length < 2) dd = "0" + dd;
??????????? h = d.Hour.ToString();
??????????? if (h.Length < 2) h = "0" + h;
??????????? mm = d.Minute.ToString();
??????????? if (mm.Length < 2) mm = "0" + mm;
??????????? ss = d.Second.ToString();
??????????? if (ss.Length < 2) ss = "0" + ss;
??????????? s += y + m + dd + h + mm + ss;
??????????? s += ra.Next(100, 999).ToString();
??????????? return s;
??????? }
??????? #endregion
??????? #region 取得文件后綴
??????? /**********************************
???????? * 函數名稱:GetFileExtends
???????? * 功能說明:取得文件后綴
???????? * 參??? 數:filename:文件名稱
???????? * 調用示例:
???????? *????????? GetRemoteObj o = new GetRemoteObj();
???????? *????????? string url = @"http://www.baidu.com/img/logo.gif";
???????? *????????? string s = o.GetFileExtends(url);
???????? *????????? Response.Write(s);
???????? *????????? o.Dispose();
???????? * ********************************/
??????? /// <summary>
??????? /// 取得文件后綴
??????? /// </summary>
??????? /// <param name="filename">文件名稱</param>
??????? /// <returns></returns>
??????? public string GetFileExtends(string filename)
??????? {
??????????? string ext = null;
??????????? if (filename.IndexOf('.') > 0)
??????????? {
??????????????? string[] fs = filename.Split('.');
??????????????? ext = fs[fs.Length - 1];
??????????? }
??????????? return ext;
??????? }
??????? #endregion
??????? #region 獲取遠程文件源代碼
??????? /**********************************
???????? * 函數名稱:GetRemoteHtmlCode
???????? * 功能說明:獲取遠程文件源代碼
???????? * 參??? 數:Url:遠程url
???????? * 調用示例:
???????? *????????? GetRemoteObj o = new GetRemoteObj();
???????? *????????? string url = @"http://www.baidu.com";
???????? *????????? string s = o.GetRemoteHtmlCode(url);
???????? *????????? Response.Write(s);
???????? *????????? o.Dispose();
???????? * ********************************/
??????? /// <summary>
??????? /// 獲取遠程文件源代碼
??????? /// </summary>
??????? /// <param name="url">遠程url</param>
??????? /// <returns></returns>
??????? public string GetRemoteHtmlCode(string Url)
??????? {
??????????? string s = "";
??????????? MSXML2.XMLHTTP _xmlhttp = new MSXML2.XMLHTTPClass();
??????????? _xmlhttp.open("GET", Url, false, null, null);
??????????? _xmlhttp.send("");
??????????? if (_xmlhttp.readyState == 4)
??????????? {
??????????????? s = System.Text.Encoding.Default.GetString((byte[])_xmlhttp.responseBody);
??????????? }
??????????? return s;
??????? }
??????? #endregion
下面的代碼是使用正則表達式取得HTML中內容的代碼
Regex regex1 = new Regex(this.NameKey, RegexOptions.Singleline | RegexOptions.IgnoreCase );
MatchCollection collection1 = regex1.Matches(this.ArtHtml);
if (collection1.Count>0)
{
result=collection1[0].Result("${title}");
}
*******************************
得到HTML代碼
string strHtml;
StreamReader sr = null; //用來讀取流
StreamWriter sw = null; //用來寫文件
Encoding code = Encoding.GetEncoding("utf-8"); //定義編碼
//構造web請求,發送請求,獲取響應
??????? WebRequest HttpWebRequest = null;
??????? WebResponse HttpWebResponse = null;
??????? HttpWebRequest = WebRequest.Create(url);
??????? HttpWebResponse = HttpWebRequest.GetResponse();
??????? //獲得流
??????? sr = new StreamReader(HttpWebResponse.GetResponseStream(), code);
??????? strHtml = sr.ReadToEnd();
轉載于:https://www.cnblogs.com/hnsjack/articles/601161.html
總結
以上是生活随笔為你收集整理的Asp.net采集用到的幾個方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 防止 刷新 重做
- 下一篇: 递归用函数、存储过程实现的效果