大众计算机学习网欢迎诸位!收藏本站注 册登 陆
常用教程:基础知识网络知识操作系统WPS网页制作数据库算法网页成功之路网络安全最新技术古典mid流行midmid背景下载中心
您现在的位置:首页 > 教程 > 文章页

利用WebClient抓取网页数据

Admin | 2016-12-26 15:48:50 | ReadNums | 874 | 标签 asp.net | 打印本页
     

  一、gb2312方式获取网页源码 

            public string getGB2312HTML(string url)  

            {  
                string str;  
                using (System.Net.WebClient client = new System.Net.WebClient())  
                {  
                    using (System.IO.Stream stream = client.OpenRead(url))  
                    {  
                        using (System.IO.StreamReader reader = new System.IO.StreamReader(stream, System.Text.Encoding.GetEncoding("gb2312")))  
                        {  
                            str = reader.ReadToEnd();  
                            reader.Close();  
                        }  
                        stream.Close();  
                    }  
                }  
                return str;  
            }  
      

           二、UTF-8方式获取网页源码 

            public string getUTF8HTML(string url)  

            {  
                string str;  
                using (System.Net.WebClient client = new System.Net.WebClient())  
                {  
                    using (System.IO.Stream stream = client.OpenRead(url))  
                    {  
                        using (System.IO.StreamReader reader = new System.IO.StreamReader(stream, System.Text.Encoding.GetEncoding("UTF-8")))  
                        {  
                            str = reader.ReadToEnd();  
                            reader.Close();  
                        }  
                        stream.Close();  
                    }  
                }  
                return str;  

            } 


问题未解决:在线咨询我要在线咨询问题

网友评论

(访客)
内容实用原创,讲得很好。
20xx年x月x日
(站长)
有问题请在线咨询。
20xx年x月x日