C # HttpWebRequest Get total page size

Hi, I am trying to get the whole total page size using HttpWebRequest from image scripts etc. I need to calculate the total web page traffic if one user visits the page through a web browser. The length of the content brings me the length of the content in byte size. Actually the length of the document. But I also need to get all the traffic from images and scripts.

This is my code. Bye.

NetworkCredential cred = new NetworkCredential("username", "password"); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url); request.Proxy = new System.Net.WebProxy("proxy", true); request.Proxy.Credentials = cred; request.UserAgent = @"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5"; using (HttpWebResponse requestresponse = (HttpWebResponse)request.GetResponse()) { Headers = requestresponse.Headers; Url = requestresponse.ResponseUri; int ContentLength; if (int.TryParse(response.Headers.Get("Content-Length"), out ContentLength)) { //This is One way i get the content leangth WebPageSize = ContentLength; } //This is another way i get the content leangth WebPageSize = request.ContentLength; return ProcessContent(requestresponse); } 

In addition, the length of the contents of the header is not guaranteed that the server will respond to it.

Any suggestions?

+4
source share
2 answers

If you just need to check the size once, why not try using the Net Firebug toolbar (Firefox extension) or an equivalent tool from other browsers? It will tell you the size of all requests that are executed when the web page loads.

+3
source

If you take a measurement and can guarantee that there is no other network traffic, you can try the following:

Get TotalBytesReceived from IPv4Statistics

Open the page in WebBrowserControl and wait for all resources to load.

Get the total number of bytes again.

From these two numbers you can calculate the total size.

Here is an article about working with IPv4Statistics:

http://www.m0interactive.com/archives/2008/02/06/how_to_calculate_network_bandwidth_speed_in_c_/

0
source

Source: https://habr.com/ru/post/1390033/


All Articles