• GZIP 头解析


    在用HttpWebRequest对象时,一般我们都没有开启gzip压缩,如果服务端返回的数据比较大,这是我们需要开启gzip压缩,怎么开启呢?

    1.给HttpWebRequest对象,添加如下Header:

    request.Headers.Add("Accept-Encoding", "gzip");

    2.对接收到的流进行解码:

    private string GetResponseBody(HttpWebResponse response)
    {
    string responseBody = string.Empty;

    if (response.ContentEncoding.ToLower().Contains("gzip")) {

    using (GZipStream stream = new GZipStream(
    response.GetResponseStream(), CompressionMode.Decompress))
    {
    using (StreamReaderreader = new StreamReader(stream))
    {
    responseBody = reader.ReadToEnd();
    }
    }
    }
    else if (response.ContentEncoding.ToLower().Contains("deflate"))
    {
    using (DeflateStream stream = new DeflateStream(
    response.GetResponseStream(), CompressionMode.Decompress))
    {
    using (StreamReader reader =
    new StreamReader(stream, Encoding.UTF8))
    {
    responseBody = reader.ReadToEnd();
    }
    }
    }
    else
    {
    using (Stream stream = response.GetResponseStream())
    {
    using (StreamReader reader =
    new StreamReader(stream, Encoding.UTF8))
    {
    responseBody = reader.ReadToEnd();
    }
    }
    }
    return responseBody;
    }
  • 相关阅读:
    新autoJS写淘宝福年种福果
    autoJS写淘宝福年种福果
    简七学理财知识
    python一键搭ftp服务器
    域名伪装
    [SWPU2019]Web1
    [网鼎杯 2020 朱雀组]phpweb
    Doc
    Docker简单使用教程
    MySQL数据库基本操作
  • 原文地址:https://www.cnblogs.com/upshania/p/5521798.html
Copyright © 2020-2023  润新知