.NET HttpWebRequest的速度与浏览器 [英] .NET HttpWebRequest Speed versus Browser

查看:464
本文介绍了.NET HttpWebRequest的速度与浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有关于净HttpWebRequest的客户机的性能问题(或Web客户端,提供了类似的结果)。



如果我使用的HttpWebRequest请求一个html页面(在这种情况下news.bbc.co.uk)并分析在该响应由应用程序读出的速度(使用HttpAnalyzer),这比一个浏览器(Firefox,铬,IE)请求相同资源显著较慢(所有高速缓存清零等)。在.NET应用程序约需要1.7秒,而0.2 - 0.3秒浏览器



这是纯粹的下降速度和代码/应用程序的效率,还是有什么?其他需要考虑的因素。



代码如下:

  HttpWebRequest的要求= NULL; 

乌里优利特=新的URI(http://news.bbc.co.uk);

=请求(HttpWebRequest的)WebRequest.Create(优利特);

request.Method =GET;
request.KeepAlive = TRUE;
request.Headers [的Accept-Encoding] =gzip的,放气;

HttpWebResponse响应=(HttpWebResponse)request.GetResponse();

response.Close();


解决方案

第一次请求一个页面,.NET尝试检测代理设置。该解决方案是在一个空WebProxy对象来传递。这样,它只是连接到远程服务器,而不是自动探测代理服务器。

  HttpWebRequest的要求=(HttpWebRequest的)WebRequest.Create(优利特); 
request.Proxy =新WebProxy();


I have a question regarding the performance of the .Net HttpWebRequest client (or WebClient, gives similar results).

If I use HttpWebRequest to request an html page (in this case news.bbc.co.uk) and analyse the speed (using HttpAnalyzer) at which the response is read by the application, this is significantly slower than a browser (Firefox, Chrome, IE) requesting the same resource (all caches cleared etc). The .Net application takes approximately 1.7 seconds versus 0.2 - 0.3 seconds for a browser.

Is this purely down to the speed and efficiency of the code / application or are there any other factors to consider?

Code as follows:

HttpWebRequest request = null;

Uri uriTest = new Uri("http://news.bbc.co.uk");

request = (HttpWebRequest)WebRequest.Create(uriTest);

request.Method = "GET";
request.KeepAlive = true;
request.Headers["Accept-Encoding"] = "gzip, deflate";

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

response.Close();

解决方案

The first time you request a page, .net tries to detect proxy settings. The solution is to pass in an empty WebProxy object. This way it just connects to remote server instead of autodetecting the proxy server.

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uriTest);
request.Proxy = new WebProxy();

这篇关于.NET HttpWebRequest的速度与浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆