Tác giả: Sưu tầm
Mô tả: Return HTML Page Source From Web URL
---------------------------------------------------
Return HTML Page Source From Web URL in C#
Mã: Chọn hết
- using System.Net;
- using System.Xml;
- using System.IO;
Mã: Chọn hết
- string url = "http://caulacbovb.com";
-
- HttpWebRequest myWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
- myWebRequest.Method = "GET";
- // make request for web page
- HttpWebResponse myWebResponse = (HttpWebResponse)myWebRequest.GetResponse();
- StreamReader myWebSource = new StreamReader(myWebResponse.GetResponseStream());
- string myPageSource = myWebSource.ReadToEnd();
- myWebResponse.Close();
- // Thêm textboxt vào, gở comment để test
- //this.textBox1.Text = myPageSource;
Hoặc:
Mã: Chọn hết
-
- private string ReadHTMLCode(string URL)
- {
- try
- {
- WebClient webClient = new WebClient();
- byte[] reqHTML;
- reqHTML = webClient.DownloadData(URL);
- UTF8Encoding objUTF8 = new UTF8Encoding();
- return objUTF8.GetString(reqHTML);
- }
- catch (Exception Ex)
- {
- MessageBox.Show(Ex.Message,"Unable to open file from URL");
- }
- return "error";
- }
-
Gọi hàm :
Mã: Chọn hết
- this.textBox1.Text = ReadHTMLCode("http://caulacbovb.com");