当前位置:编程学习 > C#/ASP.NET >>

C#让WebClient使用gzip编码,并解压

 WebClient client = new WebClient();client.Headers.Add("Accept-Encoding", "gzip, deflate");string sUrl = "http://www.sohu.com";byte[] byteArray = client.DownloadData(sUrl);

  // 处理 gzip string sContentEncoding = client.ResponseHeaders["Content-Encoding"];if (sContentEncoding == "gzip")

  { ToolClass.LogMessage("gzip ok", page);MemoryStream ms = new MemoryStream(byteArray);MemoryStream msTemp = new MemoryStream();int count = 0;GZipStream gzip = new GZipStream(ms, CompressionMode.Decompress);byte[] buf = new byte[1000];

  while ((count = gzip.Read(buf, 0, buf.Length)) > 0)

  { msTemp.Write(buf, 0, count);}

  byteArray = msTemp.ToArray();} // end-gzip

  string sHtml = Encoding.GetEncoding(936)。GetString(byteArray);

补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,