Parse HTML & Fetch Resources in .NET Windows Service
I'm writing a service which fires every few minutes and fetches a given URL. I'm using the HttpWebRequest class for the HTML request. This works fine, but I also have to measure the time it would take the page to load -- i.e. how long it takes to download the CSS, JS, etc. I'd love to use the WebBrowser control, but it doesn't work w/in a service, allegedly. Does anyone know of a 3rd party component which will accomplish this? I'd like to keep it as simple as possible. The most complex solution would be using something like HTML Agility Pack to parse the HTML and fetch all the resources myself. This would requi开发者_如何学Gore substantial development to get perfect, and I'd like to find something pre-baked if possible. Having a "document finished loading" event like the WebBrowser control is a minimum. Being able to get stats on each resource downloaded would be great, but not required.
Please help -- greatly appreciated.
You have to use Html Agility Pack . Try something like this:
StreamReader responseStream = new StreamReader(webResponse.GetResponseStream(),
System.Text.Encoding.UTF8);
queryContent = responseStream.ReadToEnd();
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(queryContent);
HtmlNode bodyNode = doc.DocumentNode.SelectSingleNode("//body | //BODY");
/* */
精彩评论