You're treating the response as a UTF-8 encoded string. That's bound to fail with binary data like GZip. A quick fix is to use Encoding.GetEncoding("iso-8859-1")
instead of UTF8
in StringToByteArray
, and also pass this encoding to the StreamReader
constructor in FetchURL
.
That encoding maps bytes to codepoints one-to-one; see this answer on Stack Overflow.
A cleaner way would be to not switch back and forth between unicode characters (string
, StreamReader
) and binary data (byte[]
, MemoryStream
) in the first place. Until you have unzipped the data, it does not constitute a "string" in any meaningful way.