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
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44
