Lấy code html của 1 trang web

Trong quá trình lập trình ứng dụng, chắc hẳn các bạn đã gặp phải trường hợp phải lấy code html từ một URL trên mạng.

Để làm được việc này, bạn chỉ cần sử dụng đoạn code sau.

string url=”http://iloveit1208.blogspot.com“;

HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);

HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();

Stream stream = httpWebResponse.GetResponseStream();

StreamReader streamReader =new StreamReader(stream, Encoding.ASCII);

string html=streamReader.ReadToEnd();

Không có nhận xét nào: