C# 获取aspx页面中所有隐藏控件的两种方法

本文介绍了一种从ASPX网页中获取隐藏POST值(如ViewState)的方法,包括适用于.NET4.0及以上版本和.NET4.0以下版本的两种实现方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

适合.NET4.0 以上版本

  /// <summary>
    /// 获取ASPX页面中隐藏post值	Viewstae 等
    /// </summary>
    /// <param name="Url"></param>
    /// <returns></returns>
    public Dictionary<string, string> GetViewHiddenData(string Url)
    {
      HttpClient httpClient = new HttpClient();
      httpClient.MaxResponseContentBufferSize = 256000;
      httpClient.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36");
      HttpResponseMessage response = httpClient.GetAsync(new Uri(Url)).Result;
      var result = Regex.Matches(response.Content.ReadAsStringAsync().Result, @"<input type=""hidden""[^>]*?.*?\/>")
                              .Cast<Match>().Select(mx => mx.Groups[0].Value.TrimStart().TrimEnd()).ToList();
      Dictionary<string, string> returnHidden = new Dictionary<string, string>();
      foreach (var item in result)
      {
        //获取 隐藏域中的 id  value
        //var reg = @"(?isn)<input((?!([<>]|id=)).)+id=""(?<id>[^""<>]+)""[^<>]*?value=""(?<value>[^<>""]*)""";
        //	var keyvalue = Regex.Match(item, reg);
        //returnHidden.Add(keyvalue.Groups[1].Value, keyvalue.Groups[2].Value);
        var key = Regex.Match(item, @"<input type=""hidden""[^>]*?[^>]+?id=""([\s\S]+?)""[^>]+>").Groups[1].Value;
        var value = Regex.Match(item, @"<input type=""hidden""[^>]*?[^>]+?value=""([\s\S]+?)""[^>]+>").Groups[1].Value;
        returnHidden.Add(key, value);
      }
      //用完要记得释放
      httpClient.Dispose();
      return returnHidden;
    }

GetViewHiddenData 使用方法:

 var paramList = new List<KeyValuePair<String, String>>();
        GetViewHiddenData("http://www.sd.xd.com/sdweb/HomePage/CntrYardQuery/QueryByBlno.aspx").ToList().
ForEach(x => paramList.Add(new KeyValuePair<string, string>(x.Key, x.Value)));

适用于.NET4.0一下版本        由于.NET4.0版本一下不包含System.Net.Http.dll

/// <summary>
    /// 共用获取隐藏,适应于NET4.0一下 由于不支持.net.http而编写的方法
    /// </summary>
    /// <param name="Url"></param>
    /// <param name="locationHref"></param>
    /// <param name="encoding"></param>
    /// <returns></returns>
    public Dictionary<string, string> GetViewHiddenDataToNet40End(string Url, string encoding)
    {
      Dictionary<string, string> returnHidden = new Dictionary<string, string>();
      HttpWebRequest getRequset = (HttpWebRequest)HttpWebRequest.Create(Url);//创建http 请求
      getRequset.Method = "Get";
      getRequset.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
      getRequset.Headers.Add("Accept-Language", "zh-Hans-CN,zh-Hans;q=0.5");
      getRequset.Headers.Add("Accept-Encoding", "gzip, deflate");
      getRequset.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko";
      getRequset.KeepAlive = true;
      getRequset.AutomaticDecompression = DecompressionMethods.GZip;
      // getRequset.Host = "bizweb.qdcdc.com";
      getRequset.Headers.Add("Access-Control-Allow-Origin", "*");

      HttpWebResponse response = (HttpWebResponse)getRequset.GetResponse();
      var retString = string.Empty;
      using (Stream responsestream = response.GetResponseStream())
      {
        using (StreamReader sr = new StreamReader(responsestream, System.Text.Encoding.GetEncoding(encoding)))
        {
          retString = sr.ReadToEnd();
        }
      }
      var result = Regex.Matches(retString, @"<input type=""hidden""[^>]*?.*?\/>")
                              .Cast<Match>().Select(mx => mx.Groups[0].Value.TrimStart().TrimEnd()).ToList();
      foreach (var item in result)
      {
        //获取 隐藏域中的 id  value
        var key = Regex.Match(item, @"<input type=""hidden""[^>]*?[^>]+?id=""([\s\S]+?)""[^>]+>").Groups[1].Value;
        var value = Regex.Match(item, @"<input type=""hidden""[^>]*?[^>]+?value=""([\s\S]+?)""[^>]+>").Groups[1].Value;
        returnHidden.Add(key, value);
      }
      return returnHidden;
    }

GetViewHiddenDataToNet40End 使用方法:

 var postLoginUrl = "http://www.xx.com.cn/zcxt/login.aspx";
      List<KeyValuePair<String, String>> paramList = new List<KeyValuePair<String, String>>();
      //处理.net http在.net4.0以下版本没有,xp 2003不运行
      // GetViewHiddenData(postLoginUrl).ToList().ForEach(x => paramList.Add(new KeyValuePair<string, string>(x.Key, x.Value)));
      GetViewHiddenDataToNet40End(postLoginUrl, "utf-8").ToList().ForEach(x => paramList.Add(new KeyValuePair<string, string>(x.Key, x.Value)));

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值