c#关于Content-Encoding: gzip的读取方法
一个网站返回的response,CharacterSet = "gb2312",读取时编码用了Encoding.GetEncoding("gb2312"),转成字符串后乱码,然后发现ContentEncoding = "gzip",搜索以后这个格式是压缩过的 https://www.cnblogs.com/lexus/archive/2013/04/03/2997451.html,这边解决方法是python的,又找了c#的 https://blog.csdn.net/apple151128/article/details/46708491:
private string GZipDecompress(Stream webStream) { using (System.IO.Compression.GZipStream stream = new System.IO.Compression.GZipStream(webStream, System.IO.Compression.CompressionMode.Decompress)) { stream.Flush(); byte[] decompressBuffer = ToByteArray(stream); int nSizeIncept = decompressBuffer.Length; stream.Close(); return System.Text.Encoding.GetEncoding("gb2312").GetString(decompressBuffer, 0, nSizeIncept); //转换为普通的字符串,这里编码可以根据CharacterSet修改 } } private byte[] ToByteArray(Stream stream) { byte[] buffer = new byte[32768]; using (MemoryStream ms = new MemoryStream()) { while (true) { int read = stream.Read(buffer, 0, buffer.Length); if (read <= 0) return ms.ToArray(); ms.Write(buffer, 0, read); } } }
另:中文乱码 https://blog.csdn.net/yewuzhitou/article/details/106387718