码迷,mamicode.com
首页 > Web开发 > 详细

webclient 比浏览器加载页面慢的一个问题

时间:2016-07-08 13:39:23      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:

测试中发现webclient 比浏览器加载页面慢的一个问题;原因WebClient 支持 gzip, deflate,但是未设置

解决方案:

class WebClientEx : WebClient
{
int timeout = 60;
public WebClientEx(int timeout = 60)
{
this.timeout = timeout * 1000;
this.Encoding = Encoding.UTF8;   
}
protected override WebRequest GetWebRequest(Uri address)
{
HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address);
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
request.Timeout = timeout;
request.ReadWriteTimeout = timeout;
return request;
}
}

 

另HttpClient中加入支持gzip\deflate:

 

var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate };

 

var httpClient = new HttpClient(handler);

 

webclient 比浏览器加载页面慢的一个问题

标签:

原文地址:http://www.cnblogs.com/94cool/p/5652887.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!