1. 程式人生 > >C# 獲取頁面的響應時間

C# 獲取頁面的響應時間

HttpWebRequest 請求:
DateTime start_time = DateTime.Now;
            try
            {
                HttpWebRequest webrequest = (HttpWebRequest)HttpWebRequest.Create("訪問地址");
                HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();
                if (webresponse.StatusCode == HttpStatusCode.OK)
                {
                    DateTime end_time = DateTime.Now;
                    TimeSpan ts = end_time - start_time;
                    int ch = (int)ts.TotalMilliseconds;//響應時間.(毫秒)
         }
            }
用WebBrowser開啟:
為WebBrowser新增Navigating和DocumentCompleted事件.
分別在兩個方法中記錄當前時間.最後用DocumentCompleted-Navigating的時間差即為響應時間