Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# HttpClient为每个请求添加半秒。为什么?_C#_Http_Dotnet Httpclient - Fatal编程技术网

C# HttpClient为每个请求添加半秒。为什么?

C# HttpClient为每个请求添加半秒。为什么?,c#,http,dotnet-httpclient,C#,Http,Dotnet Httpclient,我想用HttpClient从网上下载一些东西。 我注意到,在C#中,Fiddler和Stopwatch跟踪的请求时间有显著差异。在这里,看一看: [TestMethod] public void HttpClientSpeedTest() { TestHttmlClient().Wait(); Assert.IsFalse(false); } private async Task TestHttmlClient() {

我想用HttpClient从网上下载一些东西。 我注意到,在C#中,Fiddler和Stopwatch跟踪的请求时间有显著差异。在这里,看一看:

    [TestMethod]
    public void HttpClientSpeedTest()
    {
        TestHttmlClient().Wait();
        Assert.IsFalse(false);
    }

    private async Task TestHttmlClient() {
        var url = "http://www.rbc.ru";
        var timer = new Stopwatch();

        using (var client = new HttpClient())
        {
            timer.Start();
            HttpResponseMessage response = await client.GetAsync(url);
            timer.Stop();
            Debug.WriteLine(timer.Elapsed);
            timer.Start();
            string jsonstr = await response.Content.ReadAsStringAsync();
            timer.Stop();
            Debug.WriteLine(timer.Elapsed);
        }
    }
给我:00:00:00.4277788用于第一次测量-获取响应,00:00:00.4360716用于第二次测量-ReadAsStringAsync(这是累积的,所以整个请求大约0.43秒)

但是!如果我们在Fiddler中查看相同的请求,我会得到:

ClientConnected:    13:59:34.334
ClientBeginRequest: 13:59:34.364
GotRequestHeaders:  13:59:34.364
ClientDoneRequest:  13:59:34.364
Determine Gateway:  0ms
DNS Lookup:         0ms
TCP/IP Connect:     3ms
HTTPS Handshake:    0ms
ServerConnected:    13:59:34.367
FiddlerBeginRequest:13:59:34.367
ServerGotRequest:   13:59:34.367
ServerBeginResponse:13:59:34.383
GotResponseHeaders: 13:59:34.383
ServerDoneResponse: 13:59:34.440
ClientBeginResponse:13:59:34.440
ClientDoneResponse: 13:59:34.440

**Overall Elapsed:  0:00:00.076**

问题是为什么HTTPClient增加了半秒?如何避免呢?

如果你避免使用
wait
关键字,会有什么不同吗?(即,
var response=client.GetAsync(url.Result
)没有太多。C#中的00:00:00.5277214比小提琴中的0:00:00.178