Cấp bậc tác giả:

DOTNET

Các cách lấy nội dung HTML từ URL

Được viết bởi QuangIT ngày 08/04/2013 lúc 01:05 AM
Có 2 cách hay bạn có thể sẽ quan tâm.
  • 0
  • 8449

Các cách lấy nội dung HTML từ URL

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();

Nguồn bài viết: Sưu tầm

BÌNH LUẬN BÀI VIẾT

Bài viết mới nhất

LIKE BOX

Bài viết được xem nhiều nhất

HỌC HTML