国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

ASP.NET技巧:請求網址并解析返回的html

2024-07-10 13:10:24
字體:
來源:轉載
供稿:網友

  目的,把遠程服務器傳回的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
    }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 微山县| 咸丰县| 蕲春县| 宁远县| 久治县| 门头沟区| 精河县| 炎陵县| 迭部县| 高邮市| 姚安县| 四会市| 黄冈市| 墨竹工卡县| 陵川县| 鄂伦春自治旗| 抚顺县| 兴海县| 昌平区| 郑州市| 苏尼特左旗| 日喀则市| 云龙县| 微山县| 方山县| 彰武县| 佳木斯市| 神木县| 同德县| 忻州市| 侯马市| 凤庆县| 凌源市| 仁寿县| 苍梧县| 石柱| 平利县| 巨野县| 凭祥市| 仲巴县| 太保市|