Cách đơn giản thường dùng
public static string GetHTMLFromURL(string url)
{
if (url.Length == 0)
throw new Exception("Invalid Url");
string html = "";
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// get the response stream.
Stream responseStream = response.GetResponseStream();
// use a stream reader that understands UTF8
StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
html = reader.ReadToEnd();
// close the reader
response.Close();
reader.Close();
return html;
}
Có một cách khác để tải nội dung html từ 1 URL xác định trước
string url = "http://vnexpress.net/GL/Home/";
System.Net.WebClient client = new System.Net.WebClient();
System.IO.Stream stm = client.OpenRead(url);
System.IO.StreamReader reader = new System.IO.StreamReader(stm);
string htm = reader.ReadToEnd();