欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASP.NET技巧:请求网址并解析返回的html

程序员文章站 2023-12-01 21:46:46
  目的,把远程服务器传回的html,解析到类里面,为gridview等提供数据源  1 、向远程服务器post数据public int postdata(string u...

  目的,把远程服务器传回的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);
            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;
        }
        #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
    }