利用WebClient进行数据抓取

//2011-08-15 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;
        }

        //2011-08-15 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;
        }


你可能感兴趣的:(C#.NET编程)