1

I have a web browser project in C#, I am thinking such system; when user writes the url then clicks "go" button, my browser get content of written web site ( it shouldn't visit that page, I mean it shouldn't display anything), then I want look for a specific "keyword" for ex; "violence", if there exists, I can navigate that browser to a local page that has a warning. Shortly, in C#, How can I get content of a web site before visiting?...

Sorry for my english, Thanks in advance!

4
  • 1
    You can't do that in C# or any other language. It is like asking to get the contents of a file without reading it. What you seem to want is to have a proxy server that will redirect on certain inspected content. Commented Dec 17, 2012 at 21:43
  • 2
    @Oded If I read this right, it's more like asking to get the contents of a file without opening them in a text editor. Commented Dec 17, 2012 at 21:44
  • @delnan - I find the question to be unclear. If the OP wants to redirect the browser to a different page if some content exists on the page the browser is trying to access, there are existing solutions for that - using a proxy. Commented Dec 17, 2012 at 21:47
  • Problem solved Guys.. Thanks a lot to all of you, I haven't tried Hall72215's, and Jim O'Neil's solution, They're probably correct, but Giesi's solution solved my problem. But be sure that your url starts with "http://" Commented Dec 17, 2012 at 22:36

3 Answers 3

4

System.Net.WebClient:

string url = "http://www.google.com"; System.Net.WebClient wc = new System.Net.WebClient(); string html = wc.DownloadString(url); 
Sign up to request clarification or add additional context in comments.

Comments

1

You have to use WebRequest and WebResponse to load a site:

example:

string GetPageSource (string url) { HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url); webrequest.Method = "GET"; HttpWebResponse webResponse = (HttpWebResponse)webrequest.GetResponse(); string responseHtml; using (StreamReader responseStream = new StreamReader(webResponse.GetResponseStream())) { responseHtml = responseStream.ReadToEnd().Trim(); } return responseHtml; } 

After that you can check the responseHtml for some Keywords... for example with RegEx.

3 Comments

first thank you so much, My code is like that; String url= urlComboBox.Text; String html = GetPageSource(url); if (html.Contains("violence")) { MessageBox.Show("Here is forbidden", "Opps!!!"); getCurrentBrowser().Navigate(@"W:\yasak.html"); } else { getCurrentBrowser().Navigate(urlComboBox.Text); } But in first line of your method.. It gives UriException says "Unknown URI format" in ...Create(url);
Your code worked! Thank you so much @Giesi , I just forget to add "http://" tag to front of url. Then It worked well.. Big thanks for you... :)
No Problem ... that is why we are here ;)
0

You can make an HTTP request (via HttpClient to the site) and parse the results looking for the various keywords. Then you can make the decision whether or not to visibly 'navigate' the user there.

There's an HTTP client sample on Dev Center that may help.

1 Comment

just realized I was answering this in the context of Windows Store apps, but much still applies in the broader case

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.