利用WebClient进行数据抓取

来源:互联网 发布:mac用移动硬盘装win7 编辑:程序博客网 时间:2024/05/29 17:41
//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;        }


原创粉丝点击