How To Read A Web Page using C#



TODO:

Have you ever wanted to read a web page into a string using C#?

 

SOLUTION:

 

public string ReadPage(string sUrl)
{
       HttpWebRequest req = null;
       HttpWebResponse res = null;
       try
       {
             req = (HttpWebRequest)WebRequest.Create(sUrl);
             res = (HttpWebResponse)req.GetResponse();
             using (StreamReader sReader = new StreamReader(res.GetResponseStream()))
             {
                  return sReader.ReadToEnd();
             }
       }
       catch (Exception x)
       {
             LastErrorMessage = x.ToString();
             LastErrorMessageFriendly = x.Message;
             return "";
       }
}

 

 

NOTES:

There are no notes on this topic.



Pingbacks and trackbacks (1)+

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading