Dzwebs.Net

撰写电脑技术杂文十余年

利用WebClient抓取网页数据

Admin | 2016-12-26 15:48:50 | 被阅次数 | 5763

温馨提示!

如果未能解决您的问题,请点击搜索;登陆可复制文章,点击登陆

  一、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;  

            } 


该杂文来自: 网站开发杂文

上一篇:WEB通讯技术之短轮询、长轮询(comet)、长连接(SSE

下一篇:WebClient.DownloadFile方法下载服务器文件到本地

网站备案号:

网站备案号:滇ICP备11001339号-7

版权属性:

Copyright 2007-2021-forever Inc. all Rights Reserved.

联系方式:

Email:dzwebs@126.com QQ:83539231 访问统计