Copy Code code as follows:
private void GetSpeed ()
{
Link Start time
DateTime stime = DateTime.Now;
File
String url = "Yun_qi_img/test.jpg";
WebRequest myrequest = webrequest.create (URL);
Link successful
WebResponse myreponse = Myrequest.getresponse ();
Gets the size byte unit of the file
int II = Int. Parse (MyReponse.ContentLength.ToString ());
Get 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)//################ reads the file and displays 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 succeeded ... Start the download. m= "+m+" | " + A1. ToString () + "/" + A2. ToString () + "KB";//startbyte + "/" + II. ToString ();
}
Link End time
DateTime Etime=datetime.now;
TimeSpan ts = etime-stime;
Total time consuming
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 (ii/1024/speedtime);
}
Else
{
Kbps = 10000;
}
The length of the picture to display for the current flow
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+ "Picture length $" +showper+ "time-consuming $" +speedtime+ "File size $" +ii);
Response.End ();
}
Finally, a netizen gives another idea to realize:
Use a page to output a 3M page (through a loop output byte), and then asynchronously to request the page, calculate the start time and end time difference, and then to calculate.
The idea is still the same, is to use WebRequest to get the flow file is just to get response time, but not to output time (their level is limited, but I think it can be achieved). Now it's just a different way to get the output time. I am in Baidu and Google is only to find the asynchronous get text content output time can. But there are pictures that seem to be a little difficult.
But the end result was a bit of a sense of achievement. Oh...