Copy codeThe Code is as follows:
Private void getSpeed ()
{
// Link Start Time
DateTime stime = DateTime. Now;
// File
String url = "http://xxx.com/images/test.jpg ";
WebRequest myRequest = WebRequest. Create (url );
// Link successful
WebResponse myReponse = myRequest. GetResponse ();
// Obtain the object size in bytes
Int ii = int. Parse (myReponse. ContentLength. ToString ());
// Obtain the stream
Stream myStream = myReponse. GetResponseStream ();
StreamReader sr = new StreamReader (myStream );
Byte [] mbyte = new byte [ii];
Int allbyte = (int) mbyte. Length;
Int startbyte = 0;
String test = "";
While (ii> 0) // ################ read the file cyclically and display the progress .....
{
Int m = myStream. Read (mbyte, startbyte, allbyte );
If (m = 0) {break ;}
Startbyte + = m;
Allbyte-= m;
Int a1 = (int) startbyte/1024;
Int a2 = (int) ii/1024;
Test + = "connection successful .. start to download .. m = "+ m +" | "+ a1.ToString () +"/"+ a2.ToString () +" KB "; // startbyte +"/"+ ii. toString ();
}
// Link end time
DateTime etime = DateTime. Now;
TimeSpan ts = etime-stime;
// Total time consumption
Double SpeedTime = (double) ts. TotalSeconds;
Double Kbps = 0;
Double ShowPer = 0;
If (SpeedTime> 0)
{
// Network speed
Kbps = Math. Round (Math. Round (ii * 8/1024/SpeedTime * 10.5)/10 );
// Kbps = Math. Round (I/1024/SpeedTime );
}
Else
{
Kbps = 10000;
}
// Display the image length for the current traffic
ShowPer = Math. Round (Kbps/100 );
If (ShowPer <1)
ShowPer = 1;
Else if (ShowPer> 82)
ShowPer = 82;
// Network speed
String sp = (Math. Round (Kbps/8*10)/10). ToString ();
Sr. Close ();
MyStream. Close ();
MyReponse. Close ();
// Response. Write (test );
Response. Write ("network speed" + sp + "image length $" + ShowPer + "Time consumed $" + SpeedTime + "file size $" + ii );
Response. End ();
}
Finally, some netizens gave another idea of implementation:
Use a page to output a 3 m web page (output byte through a loop), then asynchronously request the page, calculate the difference between the start time and the end time, and then calculate.
The idea is still the same, that is, using webRequest to get the stream file only gets the response time, but the output time is not allowed (my own level is limited, but I think it can be implemented ). Now I just changed the method to get the output time. In baidu and google, I can only find the time for Asynchronously obtaining text content output. However, it seems a little difficult to have images.
However, the final result is still somewhat rewarding. Haha...