Jonah
Jonah

Reputation: 11

in C#, how can I get the HTML content of a website before displaying it?

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!

Upvotes: 1

Views: 4892

Answers (3)

Jim O'Neil
Jim O'Neil

Reputation: 23754

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.

Upvotes: 0

Sean Hall
Sean Hall

Reputation: 7878

System.Net.WebClient:

string url = "http://www.google.com";
System.Net.WebClient wc = new System.Net.WebClient();
string html = wc.DownloadString(url);

Upvotes: 4

Link
Link

Reputation: 1711

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.

Upvotes: 1

Related Questions