Skip to main content
 首页 » 编程设计

c#之添加 Interval 时 HttpWebRequest 变慢

2024年01月28日36Terrylee

测试下载网页源的不同可能性我得到了以下结果(到 google.com、9gag.com 的平均时间以毫秒为单位):

  • 普通 HttpWebRequest:169、360
  • Gzip HttpWebRequest: 143, 260
  • WebClient GetStream : 132, 295
  • WebClient DownloadString: 143, 389

因此,对于我的 9gag 客户端,我决定采用 gzip HttpWebRequest。问题是,在我的实际程序中实现后,请求花费了两倍多的时间。
仅在两个请求之间添加 Thread.Sleep 时也会出现问题。

编辑:
只是稍微改进了代码,仍然是同样的问题:在循环中运行时,当我在请求之间添加延迟时,请求需要更长的时间

for(int i = 0; i < 100; i++) 
{ 
    getWebsite("http://9gag.com/"); 
} 

每个请求大约需要 250 毫秒。

for(int i = 0; i < 100; i++) 
{ 
    getWebsite("http://9gag.com/"); 
    Thread.Sleep(1000); 
} 

每个请求大约需要 610 毫秒。

    private string getWebsite(string Url) 
    { 
        Stopwatch stopwatch = Stopwatch.StartNew(); 
 
        HttpWebRequest http = (HttpWebRequest)WebRequest.Create(Url); 
        http.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; 
        string html = string.Empty; 
 
        using (HttpWebResponse webResponse = (HttpWebResponse)http.GetResponse()) 
        using (Stream responseStream = webResponse.GetResponseStream()) 
        using (StreamReader reader = new StreamReader(responseStream)) 
        { 
 
            html = reader.ReadToEnd(); 
        } 
 
        Debug.WriteLine(stopwatch.ElapsedMilliseconds); 
        return html; 
    } 

有解决这个问题的想法吗?

请您参考如下方法:

也许试一试,尽管它可能只会帮助您解决单个请求的情况,而在执行多线程版本时实际上会使事情变得更糟。

ServicePointManager.UseNagleAlgorithm = false; 

这里引用自 MSDN 文档中的 HttpWebRequest Class

Another option that can have an impact on performance is the use of the UseNagleAlgorithm property. When this property is set to true, TCP/IP will try to use the TCP Nagle algorithm for HTTP connections. The Nagle algorithm aggregates data when sending TCP packets. It accumulates sequences of small messages into larger TCP packets before the data is sent over the network. Using the Nagle algorithm can optimize the use of network resources, although in some situations performance can also be degraded. Generally for constant high-volume throughput, a performance improvement is realized using the Nagle algorithm. But for smaller throughput applications, degradation in performance may be seen.

An application doesn't normally need to change the default value for the UseNagleAlgorithm property which is set to true. However, if an application is using low-latency connections, it may help to set this property to false.