利用WebClient進行資料抓取

暖楓無敵發表於2011-08-15
//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;
        }


相關文章