C#之获取网页标题...
完整源码下载地址:http://download.csdn.net/download/friendan/4337765效果截图如下:
主要代码如下:
view plain copy
print?
[*]//获取网页标题函数
[*] private String getWebTitle(String url)
[*] {
[*] //请求资源
[*] System.Net.WebRequest wb = System.Net.WebRequest.Create(url.Trim());
[*]
[*] //响应请求
[*] WebResponse webRes =null;
[*]
[*] //将返回的数据放入流中
[*] Stream webStream = null;
[*] try{
[*] webRes = wb.GetResponse();
[*] webStream = webRes.GetResponseStream();
[*] }
[*] catch(Exception e)
[*] {
[*] return "输入的网址不存在或非法...";
[*] }
[*]
[*]
[*] //从流中读出数据
[*] StreamReader sr=new StreamReader(webStream, System.Text.Encoding.Default);
[*]
[*] //创建可变字符对象,用于保存网页数据
[*] StringBuilder sb = new StringBuilder();
[*]
[*] //读出数据存入可变字符中
[*] String str = "";
[*] while ((str=sr.ReadLine())!=null)
[*] {
[*] sb.Append(str);
[*] }
[*]
[*] //建立获取网页标题正则表达式
[*] String regex = @"<title>.+</title>";
[*]
[*] //返回网页标题
[*] String title=Regex.Match(sb.ToString(),regex).ToString();
[*] title=Regex.Replace(title,@"[\""]+","");
[*] return title;
[*] }
转自:http://blog.csdn.net/friendan/article/details/7613752
页:
[1]