How to calculate total size of a page with additional files and scripts

痴心易碎 提交于 2019-12-11 09:22:13

问题


I would like to ask if it's possible to get programmatically in C#, a specific site content size. By size I mean: the full size of the site including all images and scripts referenced in the head section or body and so on. For example if we have a site http://www.google.com I want o get it's total size including the logo, scripts refered to, and so on as it will be presented to the user not just the main page.

Here is a picture what I mean: (click for full size)

If we use IE Developer tool in IE 9, and start capturing traffic on the network session, than we hit google.com and it shows the total files loaded (.js, .png, and so on) and the time of loading in milliseconds.

I tried to do something similar using a webrequest but i get only 43kb instead of 101 as IE developer tool gets.

Here is the code:

WebRequest request = WebRequest.Create(textBox2.Text.ToString());     
request.Credentials = CredentialCache.DefaultCredentials;           
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream dataStream = response.GetResponseStream();      
StreamReader reader = new StreamReader(dataStream);          
string responseFromServer = reader.ReadToEnd();         
byte[] bytes = Encoding.ASCII.GetBytes(responseFromServer);
MessageBox.Show(ConvertSize(responseFromServer.Length) + "  -  " + responseFromServer.Length.ToString());
reader.Close();
dataStream.Close();
response.Close();

How can I get the total size of a site including all images, js and additional files used/referenced in that specific page? Thanks a lot!


回答1:


Your WebRequest is just getting the HTML. It's not parsing to fetch any referenced files (images, CSS, javascript includes, etc). Controls such as the WebBrowser control can allow you to automate a browser



来源:https://stackoverflow.com/questions/11614526/how-to-calculate-total-size-of-a-page-with-additional-files-and-scripts

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!