Obtain a remote webpage using a regular expression

Source: Internet
Author: User

[A basket of meals is very rare and original. Please indicate the source for reprintingHttp://www.cnblogs.com/wanghafan/archive/2012/02/09/2344276.html]

-- Simple example: 

View Code

1 using System.Text;
2 using System.Text.RegularExpressions;
3
4 string str = "<img>123456789</img>";
5 string regexStr = @"<img>([^<]*)</img>";
6 Match mc = Regex.Match(str, regexStr, RegexOptions.IgnoreCase);
7 Response.Write(mc.Groups[1].Value);

-- Weather example: 

View Code

1 using System. Text;
2 using System. Text. RegularExpressions;
3
4 string str = GetRequestString ("http://www.ntxnw.com.cn/tq.asp? S_type = 00 ", 8000, 1, Encoding. GetEncoding (" GB2312 "));
5 string regexStr = xxxxxxxxxxxxxxxxxxxx;
6 Match mc = Regex. Match (str, regexStr, RegexOptions. IgnoreCase );
7 this. divContent. InnerHtml = mc. Groups [1]. Value;
8 /// <summary>
9 // obtain the content of the specified remote webpage
10 /// </summary>
11 /// <param name = "strUrl"> remote webpage address to be searched </param>
12 /// <param name = "timeout"> set the timeout period, which is generally set to 8000 </param>
13 /// <param name = "enterType"> whether to output a line break. 0 indicates no output. 1 indicates a line break in the output text box. </param>
14 /// <param name = "EnCodeType"> encoding method </param>
15 /// <returns> </returns>
16 /// you can also consider static string
17 public string GetRequestString (string strUrl, int timeout, int enterType, System. Text. Encoding EnCodeType)
18 {
19 string strResult;
20 try
21 {
22 HttpWebRequest myReq = (HttpWebRequest) HttpWebRequest. Create (strUrl );
23 myReq. Timeout = timeout;
24 HttpWebResponse HttpWResp = (HttpWebResponse) myReq. GetResponse ();
25 Stream myStream = HttpWResp. GetResponseStream ();
26 StreamReader sr = new StreamReader (myStream, EnCodeType );
27 StringBuilder strBuilder = new StringBuilder ();
28 while (-1! = Sr. Peek ())
29 {
30 strBuilder. Append (sr. ReadLine ());
31 if (enterType = 1)
32 {
33 strBuilder. Append ("\ r \ n ");
34}
35}
36 strResult = strBuilder. ToString ();
37}
38 catch (Exception err)
39 {
40 strResult = "request error:" + err. Message;
41}
42 return strResult;
43}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.