模拟请求(模拟header gzip解压 泛型)

Posted Lulus

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了模拟请求(模拟header gzip解压 泛型)相关的知识,希望对你有一定的参考价值。

private static T GetDataCommonMethod<T>(string url, string host, HeaderData headerData) where T : class
{
    //注意Host和请求基网址各个请求可能不同
    var client = new WebClient();
    var headerStr = $"Host: {host}" + "\n" +
                "User-Agent: ......" + "\n" +
                @"Accept: application/json, text/plain, */*" + "\n" +
                "Accept-Encoding: gzip, deflate" + "\n" +
                "Accept-Language: zh-cn" + "\n" +
                $"Cookie: m={headerData.m}; u={headerData.u}; wx={headerData.wx}; ......" + "\n";
    var rawHeaders = headerStr.Split(new[] { ‘\n‘, ‘\r‘ }, StringSplitOptions.RemoveEmptyEntries).ToList();
    var headerPairs = rawHeaders.Select(x =>
    {
        var items = x.Split(new[] { ‘:‘ }, 2, StringSplitOptions.RemoveEmptyEntries);
        var key = items[0].Trim();
        var value = items[1].Trim();
        return new ValueTuple<string, string>(key, value);
     }).ToList();
     headerPairs.ForEach(x =>
     {
         client.Headers.Set(x.Item1, x.Item2);
     });
     //先gzip解压再转string
     byte[] rawBytes = client.DownloadData(url);
     var stream = new MemoryStream(rawBytes);
     GZipStream g = new GZipStream(stream, CompressionMode.Decompress);
     //gzip最后四位是原始长度
     var length = BitConverter.ToInt32(rawBytes, rawBytes.Length - 4);
     byte[] bytes = new byte[length];
     g.Read(bytes, 0, bytes.Length);
     //编码是UTF8
     string s0 = System.Text.Encoding.UTF8.GetString(bytes);

     T result = JsonConvert.DeserializeObject<T>(s0);
     return result;
}

以上是关于模拟请求(模拟header gzip解压 泛型)的主要内容,如果未能解决你的问题,请参考以下文章

jmeter ---模拟http请求/发送gzip数据

模拟系统负载.md

php curl 模拟get请求 并设置header

Fidder模拟发送请求

GNS3模拟ASA

python模拟浏览器爬取数据