我的代码是否有任何建议。我不知道为什么表现不佳。 我的代码大约需要200毫秒而浏览器只需50毫秒,如下所示:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(url));
request.Proxy = null;
request.Method = "GET";
request.Accept = "image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, */*";
request.Headers.Add("Accept-Language", "zh-CN");
request.UserAgent = "Mozilla/5.0 (Windows NT 5.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36";
request.Headers.Add("Accept-Encoding", "gzip, deflate");
request.KeepAlive = true;
request.AllowAutoRedirect = false;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string str;
Stream st = response .GetResponseStream();
if (response .ContentEncoding.ToLower().Contains("gzip"))
{
st = new GZipStream(st, CompressionMode.Decompress);
}
StreamReader reader = new StreamReader(st);
str = reader.ReadToEnd();
reader.Close();
response.Close();
答案 0 :(得分:0)
为什么要手动处理解压缩?只需使用
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
它还会添加accept-encoding
标题,并在清除浏览器缓存后再次检查。