Click to See Complete Forum and Search --> : Displaying website source code in a richtextbox
thisismyusername2010
January 20th, 2010, 02:10 PM
How would I fetch and then display source code from a website into a richtextbox in c#?
i.e. if I were to input in "google.com" then on one side I'll have the browser that travelled to google, but on the other side is a richtextbox with the html source code from google.
rliq
January 20th, 2010, 06:41 PM
I'm interested as to why it has be be a Rich Text Box and not an ordinary Text Box. Are you expecting the syntax of the HTML source code to be automatically highlighted in the Rich Text Box? As that will not happen and it will take you a looooooooooooooong time to write the code to get it to happen perfectly.
However, to get the source code of a URL, you may wish to try:
public static String ReadAllText(String url)
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
using (WebResponse webResponse = webRequest.GetResponse())
{
using (StreamReader responseReader = new StreamReader(webResponse.GetResponseStream()))
{
return responseReader.ReadToEnd();
}
}
}
nelo
January 20th, 2010, 06:45 PM
Hi,
You can also use the WebClient class from the System.Net namespace. Here's a link to the details in MSDN (http://msdn.microsoft.com/en-us/library/system.net.webclient.aspx). I think I like rliq's suggestion (i.e. WebRequest approach) though...I was looking for that class but missed it somehow. Good spot rliq.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.