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;
}