HttpClient with infinite time out throws time out exception

后端 未结 3 1310
被撕碎了的回忆
被撕碎了的回忆 2021-02-15 18:13

My HttpClient uses digest authentication to connect to the server and expects search queries in response. These search queries can come in any time so the client is expected to

3条回答
  •  感情败类
    2021-02-15 18:41

    Although the default value for Stream.CanTimeout is false, returning a stream via the response.Content.ReadAsStreamAsync() gives a stream where the CanTimeout property returns true.

    The default read and write time out for this stream is 5 minutes. That is after five minutes of inactivity, the stream will throw an exception. Much similar to the exception shown in the question.

    To change this behavior, ReadTimeout and/or the WriteTimeout property of the stream can be adjusted.

    Below is the modified version of the ListenForSearchQueries method that changes the ReadTimeout to Infinite.

    public static async void ListenForSearchQueries(int resourceId)
    {
        var url = $"xxx/yyy/{resourceId}/waitForSearchRequest?token=abc";
    
        var httpHandler = new HttpClientHandler { PreAuthenticate = true };
    
        using (var digestAuthMessageHandler = new DigestAuthMessageHandler(httpHandler, "user", "password"))
        using (var client = new HttpClient(digestAuthMessageHandler))
        {
            client.Timeout = TimeSpan.FromMilliseconds(Timeout.Infinite);
    
            var request = new HttpRequestMessage(HttpMethod.Get, url);
    
            var tokenSource = new CancellationTokenSource();
                tokenSource.CancelAfter(TimeSpan.FromMilliseconds(Timeout.Infinite));
    
            using (var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, tokenSource.Token))
            {
                Console.WriteLine("\nResponse code: " + response.StatusCode);
    
                using (var body = await response.Content.ReadAsStreamAsync())
                {
                    body.ReadTimeout = Timeout.Infinite;
    
                    using (var reader = new StreamReader(body))
                        while (!reader.EndOfStream)
                            Console.WriteLine(reader.ReadLine());
                }
             }
        }
    }
    

    This fixed the exception which was actually being thrown by the stream but seemed like was being thrown by the HttpClient.

提交回复
热议问题