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}
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}