请求网址并解析返回的html

2016-02-19 15:30 36 1 收藏

今天图老师小编要向大家分享个请求网址并解析返回的html教程,过程简单易学,相信聪明的你一定能轻松get!

【 tulaoshi.com - Web开发 】

  目的,把远程服务器传回的Html,解析到类里面,为GridView等提供数据源
  1 、向远程服务器Post数据
  public int PostData(string url, string data, out string info)
          {

              info = "";
              CookieContainer cc = new CookieContainer();
              HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
              request.CookieContainer = cc;
              request.Method = "POST";
              request.ContentType = "application/x-www-form-urlencoded";
              Stream requestStream = request.GetRequestStream();
              byte[] byteArray = Encoding.UTF8.GetBytes(data);
              requestStream.Write(byteArray, 0, byteArray.Length);
              requestStream.Close();
              HttpWebResponse response = request.GetResponse() as HttpWebResponse;
              Uri responseUri = response.ResponseUri;
              Stream receiveStream = response.GetResponseStream();
              Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
              StreamReader readStream = new StreamReader(receiveStream, encode);
              string result = readStream.ReadToEnd();
              info = result;
              return 0;

          }2、解析返回的html,有省略
   public ClassInfo[] GetClass(string html)
          {

              ArrayList ar = new ArrayList();
              ArrayList arr = new ArrayList();
              string table = "";
              Regex regtable = new Regex(@"(?=table.*).*?(?=/table)", RegexOptions.Singleline);
              Match ma = regtable.Match(html);
              while (ma.Success)
              {
                  if (ma.Value.Trim() != "")
                  {
                      arr.Add(HttpUtility.HtmlDecode(ma.Value));
                  }
                  ma = ma.NextMatch();
              }
              for (int i = 0; i arr.Count; i++)
              {
                  table = arr[i].ToString() + table;
              }

  
              Regex reg = new Regex(@"(?=.*?).*?(?=.*?)", RegexOptions.Singleline);
              Match m = reg.Match(table);

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/webkaifa/)

  
              while (m.Success)
              {
                  if (m.Value.Trim() != "")
                  {

                      ar.Add(HttpUtility.HtmlDecode(m.Value));
                  }
                  m = m.NextMatch();
              }

  
             ClassInfo[] ci = new ClassInfo[classno];
              for (int i = 0; i classno; i++)
              {
                  ci[i] = new ClassInfo();
                  ci[i].RegisterDate = (ar[i * 8 + 0]).ToString();

                  ci[i].LoginDate = (ar[i * 8 + 1]).ToString();
                  ci[i].LogoutDate = (ar[i * 8 + 2]).ToString();
                  ci[i].UseMin = ar[i * 8 + 3].ToString();
                  ci[i].ClassName = ar[i * 8 + 5].ToString();
                  ci[i].ClassType = ar[i * 8 + 6].ToString();
                  ci[i].Percent = ar[i * 8 + 7].ToString();

              }

   

              return ci;

  
          }
  3、定义ClassInfo类(课程类),用CodeSmith生成
   public class ClassInfo
      {
          Member Variables#region Member Variables

  
          protected string _loginDate;
          protected string _LogoutDate;
          protected string _registerDate;
          protected string _useMin;
          protected string _className;
          protected string _classType;
          protected string _percent;
          //protected string _nouse;
          #endregion

          Constructors#region Constructors

          public ClassInfo() { }

          public ClassInfo(string loginDate, string LogoutDate, string registerDate, string useMin, string className, string classType, string percent)
          {
              this._loginDate = loginDate;
              this._LogoutDate = LogoutDate;
              this._registerDate = registerDate;
              this._useMin = useMin;
              this._className = className;
              this._classType = classType;
              this._percent = percent;
          }

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/webkaifa/)

          #endregion

          Public Properties#region Public Properties
          //        public string NoUse
          //        {
          //            get{return _nouse;}
          //            set{ _nouse= value;}
          //        }

   

          public string LoginDate
          {
              get { return _loginDate; }
              set { _loginDate = value; }
          }

          public string LogoutDate
          {
              get { return _LogoutDate; }
              set { _LogoutDate = value; }
          }

          public string RegisterDate
          {
              get { return _registerDate; }
              set { _registerDate = value; }
          }

          public string UseMin
          {
              get { return _useMin; }
              set { _useMin = value; }
          }

          public string ClassName
          {
              get { return _className; }
              set
              {
                  if (value != null && value.Length 50)
                      throw new ArgumentOutOfRangeException("Invalid value for ClassName", value, value.ToString());
                  _className = value;
              }
          }

          public string ClassType
          {
              get { return _classType; }
              set
              {
                  if (value != null && value.Length 50)
                      throw new ArgumentOutOfRangeException("Invalid value for ClassType", value, value.ToString());
                  _classType = value;
              }
          }

          public string Percent
          {
              get { return _percent; }
              set
              {
                  if (value != null && value.Length 50)
                      throw new ArgumentOutOfRangeException("Invalid value for Percent", value, value.ToString());
                  _percent = value;
              }
          }

          #endregion
      }

  http://bluewater.cnblogs.com/archive/2006/06/19/429720.html

来源:http://www.tulaoshi.com/n/20160219/1609783.html

延伸阅读
标签: Web开发
一、AjaxJson.aspx 处理业务数据,产生JSon数据,供JqueryRequest.aspx调用,代码如下: 代码如下: protected void Page_Load(object sender, EventArgs e) { string u = Request["UserName"]; string p = Request["Password"]; string output = string.Format("'UserName':'{0}','Password':'{1}'", u, p); Response.Write("[{...
标签: Web开发
一、WebService.asmx 处理业务数据,在GetWhether方法中产生天气情况数据,供JqueryRequest.aspx调用,代码如下: 代码如下: [System.Web.Script.Services.ScriptService] public class WebService : System.Web.Services.WebService { public WebService () { //如果使用设计的组件,请取消注释以下行 //InitializeComponent(); ...
标签: 电脑入门
如需查找您在几天前、几周前甚至几个月前访问过、但又不曾添加到Favorites(收藏夹)内的Web页面,则请在Windows XP中借助Internet Explorer 6所提供的History(历史记录)列表进行追溯。单击工具栏上的History(历史)按钮,浏览器便会显示出历史记录,并提供与您今天、昨天、前天和此前三周所访问过的每个页面相对应的快捷方式。这些链接将按...
存储过程 p_sys_Login 定义如下: CREATE PROCEDURE p_sys_Login @argUserID varchar(20), --用户名 @argPassword varchar(20), --密码 @argResult varchar(50) OUTPUT --登录结果AS/* ... ...*/ 下面演示如何在C#中用最简洁有效的代码执行该存储过程并返回数据: /// /// 用户登录验证/// /// 用户名 /// 密码 public void Login(string ...
标签: Web开发
一、WebService.asmx: 处理业务数据,在GetList方法中产生泛型集合数据,供JqueryRequest.aspx调用,代码如下: [WebMethod] 代码如下: public Liststring GetList() { Liststring list = new Liststring(); list.Add("aaaaaaaaaaaa"); list.Add("bbbbbbbbbbbb"); list.Add("cccccccccccc"); list.Add("dddddddddddd"); retu...

经验教程

981

收藏

52
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部