olsh/curl-to-csharp

using HttpClient is an antipattern

abitrolly opened this issue · 2 comments

I've just discovered that the code generated by https://curl.olsh.me/ is subject to socket exhaustion. See https://www.aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/

using (var httpClient = new HttpClient())
{
    using (var request = new HttpRequestMessage(new HttpMethod("POST"), "https://sentry.io/api/0/projects/1/groups/?status=unresolved"))
    {
        request.Headers.TryAddWithoutValidation("Accept", "application/json");
        request.Headers.TryAddWithoutValidation("User-Agent", "curl/7.60.0"); 

        var base64authorization = Convert.ToBase64String(Encoding.ASCII.GetBytes("username:password"));
        request.Headers.TryAddWithoutValidation("Authorization", $"Basic {base64authorization}"); 

        request.Content = new StringContent("{\"status\": \"resolved\"}");
        request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json"); 

        var response = await httpClient.SendAsync(request);
    }
}
olsh commented

Hi @abitrolly

Thank you for the feedback! 👍🏻
This is a duplicate #26
Feel free to propose your solution.

I am too new to C# to propose anything. :D