{"record":{"id":"5d3d64508be26eaf","repo":"BCUninstaller/Bulk-Crap-Uninstaller","slug":"the-server-returned-data-in-an-unknown-encoding","errorCode":null,"errorMessage":"The server returned data in an unknown encoding: ","messagePattern":"The server returned data in an unknown encoding: ","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"warning","filePath":"source/KlocTools/Extensions/WebExtensions.cs","lineNumber":53,"sourceCode":"                    p => p.TrimStart().StartsWith(\"charset\", StringComparison.InvariantCultureIgnoreCase));\n            if (charsetPart == null)\n                return defaultEncoding;\n\n            var charsetPartParts = charsetPart.Split('=');\n            if (charsetPartParts.Length != 2)\n                return defaultEncoding;\n\n            var charsetName = charsetPartParts[1].Trim();\n            if (charsetName == \"\")\n                return defaultEncoding;\n\n            try\n            {\n                return Encoding.GetEncoding(charsetName);\n            }\n            catch (ArgumentException ex)\n            {\n                throw new InvalidOperationException(\"The server returned data in an unknown encoding: \" + charsetName, ex);\n            }\n        }\n\n        public static string DownloadStringAwareOfEncoding(this WebClient webClient, Uri uri)\n        {\n            var rawData = webClient.DownloadData(uri);\n            var encoding = GetEncodingFrom(webClient.ResponseHeaders, Encoding.UTF8);\n            return encoding.GetString(rawData);\n        }\n    }\n}\n","sourceCodeStart":35,"sourceCodeEnd":65,"githubUrl":"https://github.com/BCUninstaller/Bulk-Crap-Uninstaller/blob/608321de98e92297377b1eb69029af55c25504a1/source/KlocTools/Extensions/WebExtensions.cs#L35-L65","documentation":"GetEncodingFrom parses the HTTP Content-Type header's charset token and calls Encoding.GetEncoding(name). If the name is not a recognized encoding, GetEncoding throws ArgumentException, rethrown here as InvalidOperationException naming the offending charset. Used by DownloadStringAwareOfEncoding to decode response bodies.","triggerScenarios":"HTTP response with a Content-Type charset that .NET's encoding tables do not recognize, such as \"utf8\" (no hyphen), \"latin1\", or a vendor-specific label.","commonSituations":"Misconfigured web servers; non-IANA charset labels; legacy encodings not registered in the framework.","solutions":["Pass a sensible defaultEncoding (e.g. Encoding.UTF8) and let the caller fall back on exception.","Catch the InvalidOperationException and retry decoding with UTF-8.","Report the bad charset to the server maintainer so the header is corrected."],"exampleFix":"// before\nvar enc = WebExtensions.GetEncodingFrom(headers);\n// after\nEncoding enc;\ntry { enc = WebExtensions.GetEncodingFrom(headers, Encoding.UTF8); }\ncatch (InvalidOperationException) { enc = Encoding.UTF8; }","handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { return WebExtensions.GetEncodingFrom(headers, Encoding.UTF8); }\ncatch (InvalidOperationException) { return Encoding.UTF8; }","preventionTips":["Always supply a defaultEncoding to GetEncodingFrom.","Whitelist known charsets and fall back to UTF-8 otherwise."],"tags":["network","encoding","http"],"backgroundTag":null,"analyzedSha":"608321de98e92297377b1eb69029af55c25504a1","analyzedAt":"2026-08-13T12:17:35.389Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}