Get HTML from a URI


/ Published in: C#
Save to your folder(s)



Copy this code and paste it in your HTML
  1. // Taken from C# 3.0 Cookbook
  2. public static string GetHtmlFromUri(string resource)
  3. {
  4. string html = string.Empty;
  5. HttpWebRequest req = (HttpWebRequest)WebRequest.Create(resource);
  6. using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
  7. {
  8. bool isSuccess = (int)resp.StatusCode < 299 && (int)resp.StatusCode >= 200;
  9. if (isSuccess)
  10. {
  11. using (StreamReader reader = new StreamReader(resp.GetResponseStream()))
  12. {
  13. html = reader.ReadToEnd();
  14. }
  15. }
  16. }
  17. return html;
  18. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.