ASP.NET数据传递:获取META信息
ASP.NET中数据传递:获取用Meta传递的数据:
1
public static string GetMeta(string strFullUrl, string MateName)
2
{
3
//strFullUrl需要有Http前缀
4
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(strFullUrl);
5
myRequest.KeepAlive = false;
6
myRequest.Timeout = 30000;
7
myRequest.ReadWriteTimeout = 30000;
8
string content = "";
9
string strError = "";
10
try
11
{
12
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
13
StreamReader myReader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
14
//返回内容存放于content中
15
content = myReader.ReadToEnd();
16
myReader.Close();
17
myResponse.Close();
18
}
19
catch (Exception ex)
20
{
21
strError = ex.Message.ToString();
22
}
23
24
//分析返回信息中的<meta>标志中content=的内容
25
RegexOptions RxOptions = RegexOptions.IgnoreCase;
26
string StrToGetMate =
27
@"<meta Name=\""{0}\"" content[\s]?=[\s\""\']+(.*?)[\""\']+.*?>";
28
Regex myRx = new Regex(string.Format(StrToGetMate, MateName), RxOptions);
29
Match myMt = myRx.Match(content);
30
if (null != myMt)
31
{
32
//有匹配内容
33
content = myMt.Groups[1].ToString();
34
}
35
//返回内容
36
if (strError.Length > 0)
37
{
38
return strError;
39
}
40
else
41
{
42
return content;
43
}
44
}
public static string GetMeta(string strFullUrl, string MateName)2
{3
//strFullUrl需要有Http前缀4
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(strFullUrl);5
myRequest.KeepAlive = false;6
myRequest.Timeout = 30000;7
myRequest.ReadWriteTimeout = 30000;8
string content = "";9
string strError = "";10
try11
{12
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();13
StreamReader myReader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);14
//返回内容存放于content中15
content = myReader.ReadToEnd();16
myReader.Close();17
myResponse.Close();18
}19
catch (Exception ex)20
{21
strError = ex.Message.ToString();22
}23

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




浙公网安备 33010602011771号