|
本帖最后由 ReEnter 于 2013-11-4 20:56 编辑
Gzip- private void SetRequest(HttpItem item)
- {
- // 验证证书
- SetCer(item);
- //设置Header参数
- if (item.Header != null && item.Header.Count > 0) foreach (string key in item.Header.AllKeys)
- {
- request.Headers.Add(key, item.Header[key]);
- }
- // 设置代理
- SetProxy(item);
- if (item.ProtocolVersion != null) request.ProtocolVersion = item.ProtocolVersion;
- request.ServicePoint.Expect100Continue = item.Expect100Continue;
- //请求方式Get或者Post
- request.Method = item.Method;
- request.Timeout = item.Timeout;
- request.KeepAlive = item.KeepAlive;
- request.ReadWriteTimeout = item.ReadWriteTimeout;
- request.Headers["Accept-Encoding"] = "gzip, deflate";
- request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
复制代码 这里添加两行- request.Headers["Accept-Encoding"] = "gzip, deflate";
- request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
复制代码 - //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);
- }
复制代码 改为- response.GetResponseStream().CopyTo(_stream, 10240);
复制代码 因为微软自动解压了。所以不需要手工判断了。gzip后。采集性能明显提高,而且,不需手工添加Httpitem里的Head头了
|
|