在 SetRequest时添加一句代码。碰到压缩之类的,.net会自动解压
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
以下代码可以优化下。不用判断GzIP之类的了
[C#] 纯文本查看 复制代码 /// <summary>
/// 提取网页Byte
/// </summary>
/// <returns></returns>
private byte[] GetByte()
{
byte[] ResponseByte = null;
using (MemoryStream _stream = new MemoryStream())
{
//GZIIP处理
if (response.ContentEncoding != null && response.ContentEncoding.Equals("gzip", StringComparison.InvariantCultureIgnoreCase))
{
//开始读取流并设置编码方式
new GZipStream(response.GetResponseStream(), CompressionMode.Decompress).CopyTo(_stream, 10240);
}
else
{
//开始读取流并设置编码方式
response.GetResponseStream().CopyTo(_stream, 10240);
}
//获取Byte
ResponseByte = _stream.ToArray();
}
return ResponseByte;
}
[C#] 纯文本查看 复制代码 /// <summary>
/// 提取网页Byte
/// </summary>
/// <returns></returns>
private byte[] GetByte()
{
byte[] ResponseByte = null;
using (MemoryStream _stream = new MemoryStream())
{ //开始读取流
response.GetResponseStream().CopyTo(_stream, 10240);
ResponseByte = _stream.ToArray();
}
return ResponseByte;
}
|