特投网

 找回密码
 立即注册

QQ登录

只需一步,快速开始

(站外) 特投工作室

查看: 4848|回复: 0

C#之获取网页标题...

[复制链接]

2569

主题

1018

回帖

6万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
69667
QQ
发表于 2016-2-8 17:25:16 | 显示全部楼层 |阅读模式
完整源码下载地址:http://download.csdn.net/download/friendan/4337765

效果截图如下:
1338286703_8934.jpg


主要代码如下:

[csharp] 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


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

您尚未登录,请登录后浏览更多精彩内容!
 立即注册
找回密码

QQ|Archiver|手机版|小黑屋|特投网 ( 皖ICP备13010674号-1 

GMT+8, 2024-3-29 22:36 , Processed in 0.027760 second(s), 11 queries , Gzip On, Redis On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表