.NET HttpWebRequest Speed versus Browser

前端 未结 9 1033
深忆病人
深忆病人 2020-12-29 13:01

I have a question regarding the performance of the .Net HttpWebRequest client (or WebClient, gives similar results).

If I use HttpWebRequest to request an html page

9条回答
  •  隐瞒了意图╮
    2020-12-29 13:36

    What's the breakdown of that 1.7s? I suspect you are measuring the entire process?

    Using this piece of code I get about 200ms in average:

    var request = (HttpWebRequest)WebRequest.Create("http://www.bbc.co.uk/news/");
    
    var stopwatch = new Stopwatch();
    stopwatch.Start();
    
    using (var response = (HttpWebResponse)request.GetResponse())
    {
        stopwatch.Stop();
        Console.WriteLine("Elapsed: {0}ms", stopwatch.ElapsedMilliseconds);
    
        var responseStream = response.GetResponseStream();
        if (responseStream != null)
            using (var sr = new StreamReader(responseStream))
                Console.WriteLine("Title: {0}", Regex.Match(sr.ReadToEnd(), @"title>(.*)

    Edit changed the code just to measure the actual HTTP request and tried again using Fiddler as well:

    Program above: Elapsed: 78ms

    Fiddler: Overall Elapsed: 00:00:00.0620000

提交回复
热议问题