ASP.NET数据传递:获取META信息

本文介绍ASP.NET中通过Meta标签进行数据传递的方法,并提供了一个实用的C#代码示例,用于从指定URL中提取Meta标签的内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ASP.NET中数据传递:获取用Meta传递的数据:
 1public static string GetMeta(string strFullUrl, string MateName)
 2{
 3//strFullUrl需要有Http前缀
 4HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(strFullUrl);
 5myRequest.KeepAlive = false;
 6myRequest.Timeout = 30000;
 7myRequest.ReadWriteTimeout = 30000;
 8string content = "";
 9string strError = "";
10try
11{
12HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
13StreamReader myReader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
14//返回内容存放于content中
15content = myReader.ReadToEnd();
16myReader.Close();
17myResponse.Close();
18}

19catch (Exception ex)
20{
21strError = ex.Message.ToString();
22}

23
24//分析返回信息中的<meta>标志中content=的内容
25RegexOptions RxOptions = RegexOptions.IgnoreCase;
26string StrToGetMate =
27@"<meta Name=\""{0}\"" content[\s]?=[\s\""\']+(.*?)[\""\']+.*?>";
28Regex myRx = new Regex(string.Format(StrToGetMate, MateName), RxOptions);
29Match myMt = myRx.Match(content);
30if (null != myMt)
31{
32//有匹配内容
33content = myMt.Groups[1].ToString();
34}

35//返回内容
36if (strError.Length > 0)
37{
38return strError;
39}

40else
41{
42return content;
43}

44}

转载于:https://www.cnblogs.com/lixx/archive/2008/05/09/1190026.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值