Tuesday, January 24, 2012

Get HTML in C#

In many situations, I needed to fetch a simple HTML (or other type) document over the HTTP protocol in C#. So here is my final solution to the problem, that mostly will work!
Sometimes it'll give you a hard time, because some websites don't like the Header, but it's great to fetch "real" HTML from a simple server.


Usings:
 using System.Net;
using System.IO;

Code:
  String getHtml(String URL)
        {
            string result = "";
            try
            {
                // Get HTML data
                WebClient client = new WebClient();
                Stream data = client.OpenRead(URL);
                StreamReader reader = new StreamReader(data);
                string str = "";
                str = reader.ReadLine();

                while (str != null)
                {
                    result += str;
                    str = reader.ReadLine();
                }
                data.Close();
            }
            catch (WebException exp)
            {
               return exp.Message;
            }
            return result;
        }

No comments:

Post a Comment