C#根据URL获取页面数据

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ConsoleApp1
{
  public class GetHtmlValueByUrl
  {
    public static string[] GetHiddenField(string url)
    {
      WebRequest request = WebRequest.Create(url);//获取页面内容
      request.Timeout = 200000;//20秒超时 
      WebResponse response = request.GetResponse();
      Stream resStream = response.GetResponseStream();
      StreamReader sr = new StreamReader(resStream);
      string tempstr = sr.ReadToEnd();//获取页面html代码

      string imgurl = string.Empty;
      string UserCode = string.Empty;

      //匹配input值
      Regex reg_EVENTVALIDATION = new Regex("<input type=\"text\" name=\"UserCode\" id=\"UserCode\" value=\"(.*?)\" />", RegexOptions.IgnoreCase);
      Match match = reg_EVENTVALIDATION.Match(tempstr);
      if (match.Success)
      {//匹配成功
        UserCode = match.Groups[1].Value;
      }

      //根据正则匹配html代码内容
      Regex reg_img = new Regex("<img src=\"/HYW2007DataQuery/FormStatusQuery.aspx(.*?)\" align=\"absmiddle\" width=\"80\" height=\"25\" border=\"0\" />", RegexOptions.IgnoreCase);
       match = reg_img.Match(tempstr);
      if (match.Success)
      {//匹配成功
        imgurl = match.Groups[1].Value;
      }

      string[] arr = { UserCode,imgurl };
      return arr;
    }

  }
}

 

posted @ 2021-10-22 16:18  亦承  阅读(381)  评论(0)    收藏  举报