C#獲得網頁源代碼的辦法。本站提示廣大學習愛好者:(C#獲得網頁源代碼的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#獲得網頁源代碼的辦法正文
本文實例講述了C#獲得網頁源代碼的辦法。分享給年夜家供年夜家參考。詳細以下:
public string GetPageHTML(string url) { try { HttpWebRequest wr = WebRequest.Create(url) as HttpWebRequest; wr.Method = "get"; wr.Accept = "*/*"; wr.Headers.Add("Accept-Language: zh-cn"); wr.Headers.Add("UA-CPU: x86"); wr.Headers.Add("Accept-Encoding: gzip, deflate"); wr.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Embedded Web Browser from: http://bsalsa.com/; InfoPath.2; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)"; wr.KeepAlive = true; wr.ServicePoint.Expect100Continue = false; wr.AllowAutoRedirect = false; HttpWebResponse wre = wr.GetResponse() as HttpWebResponse; StreamReader sreader = new StreamReader(wre.GetResponseStream(), Encoding.GetEncoding("GBK")); string sHtml = sreader.ReadToEnd(); wre.Close(); return sHtml; } catch { return ""; } }
願望本文所述對年夜家的C#法式設計有所贊助。