webclient 比浏览器加载页面慢的一个问题
测试中发现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);
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步