BCUninstaller/Bulk-Crap-Uninstaller · warning · InvalidOperationException

The server returned data in an unknown encoding:

Error message

The server returned data in an unknown encoding: 

What it means

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.

Source

Thrown at source/KlocTools/Extensions/WebExtensions.cs:53

                    p => p.TrimStart().StartsWith("charset", StringComparison.InvariantCultureIgnoreCase));
            if (charsetPart == null)
                return defaultEncoding;

            var charsetPartParts = charsetPart.Split('=');
            if (charsetPartParts.Length != 2)
                return defaultEncoding;

            var charsetName = charsetPartParts[1].Trim();
            if (charsetName == "")
                return defaultEncoding;

            try
            {
                return Encoding.GetEncoding(charsetName);
            }
            catch (ArgumentException ex)
            {
                throw new InvalidOperationException("The server returned data in an unknown encoding: " + charsetName, ex);
            }
        }

        public static string DownloadStringAwareOfEncoding(this WebClient webClient, Uri uri)
        {
            var rawData = webClient.DownloadData(uri);
            var encoding = GetEncodingFrom(webClient.ResponseHeaders, Encoding.UTF8);
            return encoding.GetString(rawData);
        }
    }
}

View on GitHub (pinned to 608321de98)

Solutions

  1. Pass a sensible defaultEncoding (e.g. Encoding.UTF8) and let the caller fall back on exception.
  2. Catch the InvalidOperationException and retry decoding with UTF-8.
  3. Report the bad charset to the server maintainer so the header is corrected.

Example fix

// before
var enc = WebExtensions.GetEncodingFrom(headers);
// after
Encoding enc;
try { enc = WebExtensions.GetEncodingFrom(headers, Encoding.UTF8); }
catch (InvalidOperationException) { enc = Encoding.UTF8; }
Defensive patterns

Strategy: fallback

Try / catch

try { return WebExtensions.GetEncodingFrom(headers, Encoding.UTF8); }
catch (InvalidOperationException) { return Encoding.UTF8; }

Prevention

When it happens

Trigger: 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.

Common situations: Misconfigured web servers; non-IANA charset labels; legacy encodings not registered in the framework.


AI-assisted analysis of BCUninstaller/Bulk-Crap-Uninstaller@608321de98 (2026-08-13). Data as JSON: /api/errors/5d3d64508be26eaf. Report an issue: GitHub.