dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.99k stars 4.67k forks source link

System.Net.Http does not properly interpret quoted string 'charset' headers #42079

Closed Gerappa92 closed 3 years ago

Gerappa92 commented 4 years ago

Charset and problem with ReadAsStringAsync

Problem with read a content from external API

General

I'm developing application in .net core 2.2. I use external API and I had a problem with reading a response content as a string. In response in content headers i get ContentType like this: {application/json; charset="utf-8"} where the charset is cause of my problem. When I'm trying to read a content by:

response.Content.ReadAsStringAsync();

I'm getting an exception:

'"utf-8"' is not a supported encoding name. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method. Parameter name: name

I workarounded this problem by simple replace quots in response header like this:

response.Content.Headers.ContentType.CharSet = response.Content.Headers.ContentType.CharSet.Replace("\"", "");

but i'm not shure that is the best solution. Maybe .net shoudn't have problem with that? Or this external API shouldn't sent charset in quotes?

Dotnet-GitSync-Bot commented 4 years ago

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

ghost commented 4 years ago

Tagging subscribers to this area: @dotnet/ncl See info in area-owners.md if you want to be subscribed.

GrabYourPitchforks commented 4 years ago

NCL, looks like quotes around the charset are valid per RFC 2045, Sec. 5.1; and RFC 2616, Sec. 14.17, 3.7, 3.6.

GrabYourPitchforks commented 4 years ago

FYI @Gerappa92, .NET Core 2.2 is out of support and is no longer receiving security updates or bug fixes. The current supported versions are .NET Core 2.1 and .NET Core 3.1. See https://dotnet.microsoft.com/platform/support/policy/dotnet-core for more information on the support policy.

geoffkizer commented 4 years ago

Yep, looks like we should support this. I assume this still exists in 5.0?

scalablecory commented 4 years ago

We definitely should support a quoted string here. https://tools.ietf.org/html/rfc7231#section-3.1.1.1

karelz commented 3 years ago

I was able to reproduce it with .NET Core 2.1 (using our LoopbackServer):

System.InvalidOperationException: The character set provided in ContentType is invalid. Cannot read content as string using an invalid character set.
---> System.ArgumentException: '"utf-8"' is not a supported encoding name. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method.
Parameter name: name
   at System.Text.EncodingTable.internalGetCodePageFromName(String name)
   at System.Text.EncodingTable.GetCodePageFromName(String name)
   at System.Text.Encoding.GetEncoding(String name)
   at System.Net.Http.HttpContent.ReadBufferAsString(ArraySegment`1 buffer, HttpContentHeaders headers)

However, it works just fine with .NET Core 3.0, 3.1 and with .NET 5. Closing as addressed. Please update to newer version of .NET Core and all should be fine for you. Thanks!