I want to create webbrowser (with the help of .Net Webbrowser control) whose requests can be controlled.
for example,
I could manage to prevent image load by removing img src.
But I am not able to find some way to limit request of webbrowser control for css files, js files and requests to image files used in CSS file.
This restriction is conditional. For some website I want everything natural but for some web site it should be configurable.
Reason: Want to send less requests to server, increase response time and reduce the traffic.
I am using below code to prevent loading images. Same way I want some logic which will prevent sending requests to css or any other files that I can decide.
private void webBrowser1_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e) { if (webBrowser1.Document != null) { foreach (HtmlElement imgElemt in webBrowser1.Document.Images) { if (imgElemt.Id == "divImagePath") { continue; } imgElemt.SetAttribute("src", ""); } } }
You would be better off to use the System.Net.Http.HttpClient
class to request the page. That way you would get the web page served to you. You could parse the page for what you do want from the site and request those items the same way.
HttpClient client = new HttpClient();
try
{
// Make an asynchronous call to get the web page
HttpResponseMessage response = await client.GetAsync("http://www.msn.com/");
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
// Do something more useful with the returned page
Console.WriteLine(responseBody);
}
catch(HttpRequestException e)
{
Console.WriteLine("nException Caught!");
Console.WriteLine("Message :{0} ", e.Message);
}
// Need to call dispose on the HttpClient object when we
// are done using it, so the app doesn't leak resources
client.Dispose(true);
If you need to display what you have pulled, you could show it in a WebBrowser control on a form. But that will pull all of the content associated with the page.