• (收集)返回字节数组


    private static byte[] getBytes(string url,CookieContainer cookie)
    {
        int c = url.IndexOf("/", 10);
        byte[] data = null;
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.CookieContainer = cookie;
        request.Referer = (c > 0 ? url.Substring(0, c) : url);
        request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
        request.Headers[HttpRequestHeader.AcceptEncoding] = "gzip, deflate";
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        string ce = response.Headers[HttpResponseHeader.ContentEncoding];
        int ContentLength = (int)response.ContentLength;
        Stream s = response.GetResponseStream();
        c = 1024 * 10;
        if (ContentLength < 0)
        {
            data = new byte[c];
            MemoryStream ms = new MemoryStream();
            int l = s.Read(data, 0, c);
            while (l > 0)
            {
                Console.WriteLine("1--> " + l);
                ms.Write(data, 0, l);
                l = s.Read(data, 0, c);
            }
            data=ms.ToArray();
            ms.Close();
        }
        else
        {
            data = new byte[ContentLength];
            int pos = 0;
            while (ContentLength > 0)
            {
                int l = s.Read(data, pos, ContentLength);
                pos += l;
                ContentLength -= l;
                Console.WriteLine("2--> " + l);
            }
        }
        s.Close();
        response.Close();
        if (ce == "gzip")
        {
            Console.WriteLine("/n/n正在解压数据...");
            MemoryStream js = new MemoryStream();           // 解压后的流
            MemoryStream ms = new MemoryStream(data);       // 用于解压的流
            GZipStream g = new GZipStream(ms, CompressionMode.Decompress);
            byte[] buffer = new byte[c];                    // 读数据缓冲区   
            int l = g.Read(buffer, 0, c);                   // 一次读 10K   
            while (l > 0)
            {
                Console.WriteLine("3--> " + l);
                js.Write(buffer, 0, l);
                l = g.Read(buffer, 0, c);
            }
            g.Close();
            ms.Close();
            data = js.ToArray();
            js.Close();
        }
        return data;
    }
  • 相关阅读:
    1.Spring MVC详解
    servlet的九大内置对象
    Hibernate设置事务的隔离级别
    wamp下php报错session_start(): open(d:/wamp/tmpsess_ku776hvb06ko4lv9d11e7mnfj1, O_RDWR) failed: No such file or directory
    json_decode()相关报错
    wamp下var_dump()相关问题
    es6箭头函数内部判断
    Json数组对象取值
    npm指向淘宝源
    APICloud之封装webApp
  • 原文地址:https://www.cnblogs.com/Luouy/p/2098473.html
Copyright © 2020-2023  润新知