Click here to Skip to main content
15,909,645 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So i've been looking at screen scrapping web pages in c# and I used some example code from the internet to do this and it works. But what I want to do now is to display the web page in a browser control on a windows form, how would I do this?

heres my code i've used
private void Page_Load(object sender, System.EventArgs e)
{
//Retrieve URL from user input box
if(Page.IsPostBack)
litHTMLfromScrapedPage.Text = GetHtmlPage( tbURL.Text );
}
public String GetHtmlPage(string strURL)
{
// the html retrieved from the page
String strResult;
WebResponse objResponse;
WebRequest objRequest = System.Net.HttpWebRequest.Create(strURL);
objResponse = objRequest.GetResponse();
// the using keyword will automatically dispose the object 
// once complete
using (StreamReader sr = 
new StreamReader(objResponse.GetResponseStream()))
{
strResult = sr.ReadToEnd();
// Close and clean up the StreamReader
sr.Close();
}
return strResult;
}
Posted

1 solution

It's a bit of a pain, depending on whether you have links to images, or anything else with relative paths.

If you don't, then it's pretty easy:
myWebbrowser.DocumentText = myString
will specify what you want to display if you have the HTML in "myString".

If you do, then you need to create an HTML file, and then use:
myWebBrowser.Navigate(@"file:\\" + path);
 
Share this answer
 
Comments
programmer1234 11-May-11 10:01am    
so for mine would I use myBrowser.tburl.text = GetHtmlPage
OriginalGriff 11-May-11 10:06am    
If I knew what "tburl" was, then maybe I could comment!
programmer1234 11-May-11 10:11am    
tburl is the text box which the user enters the web page address in
OriginalGriff 11-May-11 10:18am    
AH! You want to display the raw page from the website?
In which case, you can feed the html string you return from GetHtmlPage into a WebBrowser Control via the DocumentText property, or you can just feed the URL of the web page into it via the URL property.

Just feeding html into a TextBox will NOT result in anything very readable, particularly if it is a single line TextBox.
programmer1234 11-May-11 10:28am    
so would that line of code be acceptable then?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900