.NET HttpClient hangs after several requests (unless Fiddler is active)

后端 未结 1 1657
情歌与酒
情歌与酒 2021-01-18 00:37

I am using System.Net.Http.HttpClient to post a sequence of requests from a console application to a REST API and to deserialize the JSON responses into strongl

相关标签:
1条回答
  • 2021-01-18 00:59

    You are not disposing of the HttpResponseMessage object. This can leave open streams with the server, and after some quota of streams with an individual server is filled, no more requests will be sent.

    using (var client = new HttpClient())
    {
        var content = new StringContent(data, Encoding.UTF8, "text/html");
        using(var response = client.PostAsync(url, content).Result)
        {    
            response.EnsureSuccessStatusCode();
            return response.Content.ReadAsAsync<MyClass>().Result;
        }
    }
    
    0 讨论(0)
提交回复
热议问题