四虎精品视频-四虎精品成人免费网站-四虎黄色网-四虎国产视频-国产免费91-国产蜜臀97一区二区三区

ASP.NET技巧:請求網(wǎng)址并解析返回的html

  目的,把遠(yuǎn)程服務(wù)器傳回的Html,解析到類里面,為GridView等提供數(shù)據(jù)源

  1 、向遠(yuǎn)程服務(wù)器Post數(shù)據(jù)

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
    }

 

AspNet技術(shù)ASP.NET技巧:請求網(wǎng)址并解析返回的html,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 狼来了电影免费观看| 女生把人吃到胃里消化第一视角| 电视剧《反击》主要演员| 土耳其电影水与火| 髋关节置换术后护理ppt| 用力快点| 90后30岁网名| 美妙天堂第三季| 蜡笔小新日语原版| 绫濑| 阿妹的诺言| k总直播间| 内蒙古电视台| 国家励志奖学金个人主要事迹1500字 | 刑事侦缉档案1演员表| 唐人街探案5免费完整观看| 画江湖之不良人电影| 森林运动会作文三年级300字 | 一直很安静简谱| 恋爱三万英尺| 澳门风云3演员表| 直播惊魂夜| 在线黄色免费网站| 邓为个人资料及简历| 敦君和女朋头| 秦腔《铡美案》全本| 贾冰又出新的喜剧电影| 红灯区未删减版| 零下的风 完整版| 视频www| 拔萝卜电影| 日本电影致命诱惑| 戒色视频| 媳妇的全盛时代| 茅山道士在线观看| 王渝萱最火的三部电影| 李玟雨| 《重紫》电视剧| 男操女视频免费| 赵元帅财神经全文| 吻胸亲乳激情大尺度|