码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-11-21 14:43:57      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:serial   efi   raw   convert   memory   sel   eth   .text   air   

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解压 泛型)

标签:serial   efi   raw   convert   memory   sel   eth   .text   air   

原文地址:http://www.cnblogs.com/Lulus/p/7872252.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!