none
VisualStudio2015,能否在调试的时候模拟慢网速吗? RRS feed

  • 问题

  • 我们在开发从服务器下载数据的程序,由于网络太快下载过程一下子就结束了,没法分析其过程。

    而我用Chrome浏览器去开发网页的时候,他的F12工具自带了一个网速模拟功能,可以将网络模拟为2G/3G等速度,极大方便了开发者。

    请问VS2015里面有没有类似的功能?


    da jia hao!

    2017年1月24日 4:09

全部回复

  • 请问你具体是什么应用场景?

    可以用线程阻塞的模式来模拟网速慢:

    while(m.Read(buffer,0,1024)>0){

    Thread.Sleep(1000); //阻塞一秒

    }

    2017年1月25日 1:23
  • 你好,

    同意Johan Shen 的回答,可以通过WebResponse, 来得到下载的数据。请参考下面的代码。

    public static int DownloadFile(String remoteFilename, String localFilename) { // Function will return the number of bytes processed // to the caller. Initialize to 0 here. int bytesProcessed = 0; // Assign values to these objects here so that they can // be referenced in the finally block Stream remoteStream = null; Stream localStream = null; WebResponse response = null; // Use a try/catch/finally block as both the WebRequest and Stream // classes throw exceptions upon error try { // Create a request for the specified remote file name WebRequest request = WebRequest.Create(remoteFilename); if (request != null) { // Send the request to the server and retrieve the // WebResponse object response = request.GetResponse(); if (response != null) { // Once the WebResponse object has been retrieved, // get the stream object associated with the response's data remoteStream = response.GetResponseStream(); // Create the local file localStream = File.Create(localFilename); // Allocate a 1k buffer byte[] buffer = new byte[1024]; int bytesRead; // Simple do/while loop to read from stream until // no bytes are returned do { // Read data (up to 1k) from the stream bytesRead = remoteStream.Read(buffer, 0, buffer.Length); // Write the data to the local file localStream.Write(buffer, 0, bytesRead); // Increment total bytes processed bytesProcessed += bytesRead;

    Thread.Sleep(1000); //阻塞一秒 } while (bytesRead > 0); } } } catch (Exception e) { Console.WriteLine(e.Message); } finally { if (response != null) response.Close(); if (remoteStream != null) remoteStream.Close(); if (localStream != null) localStream.Close(); } // Return total bytes processed to caller. return bytesProcessed; }


    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2017年1月25日 5:41
    版主
  • 这种方法我也会,也用过,但是很不好用。我很奇怪vs作为一个老牌开发工具,为何这个基本功能都没有。。。

    用过chrome浏览器F12的开发人员就会知道如果开发工具本身提供这个功能,是一个多么美妙的事情。


    da jia hao!

    2017年2月5日 6:55