【问题标题】:Fastest C# Code to Download a Web Page下载网页的最快 C# 代码
【发布时间】:2010-09-06 18:36:36
【问题描述】:

给定一个 URL,下载该网页内容的最有效代码是什么?我只考虑 HTML,而不是关联的图像、JS 和 CSS。

【问题讨论】:

    标签: c#


    【解决方案1】:
    public static void DownloadFile(string remoteFilename, string localFilename)
    {
        WebClient client = new WebClient();
        client.DownloadFile(remoteFilename, localFilename);
    }
    

    【讨论】:

    • 这是最慢的!,实例化一个新的 WebClient 在实际下载之前有 3-5 个延迟我听说这是由于检查代理支持。我建议使用 Socket 方法下载,因为这是最快的解决方案
    • 我将最快解释为“尽可能少的代码字母”。
    • @SSpoke 不必每次下载站点都使用新实例,可以使用Webclient的静态实例来避免延迟。
    【解决方案2】:

    System.Net.WebClient

    来自 MSDN:

    using System;
    using System.Net;
    using System.IO;
    
    public class Test
    {
        public static void Main (string[] args)
        {
            if (args == null || args.Length == 0)
            {
                throw new ApplicationException ("Specify the URI of the resource to retrieve.");
            }
            WebClient client = new WebClient ();
    
            // Add a user agent header in case the 
            // requested URI contains a query.
    
            client.Headers.Add ("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
    
            Stream data = client.OpenRead (args[0]);
            StreamReader reader = new StreamReader (data);
            string s = reader.ReadToEnd ();
            Console.WriteLine (s);
            data.Close ();
            reader.Close ();
        }
    }
    

    【讨论】:

    • 希望 MSDN 在他们的示例中真正处理 IDisposable 资源。一个小例外,Stream/StreamReader 将不会被清理。 using 是你的朋友。
    【解决方案3】:

    使用 System.Net 中的 WebClient 类;在 .NET 2.0 及更高版本上。

    WebClient Client = new WebClient ();
    Client.DownloadFile("http://mysite.com/myfile.txt", " C:\myfile.txt");
    

    【讨论】:

      【解决方案4】:

      WebClient.DownloadString

      public static void DownloadString (string address)
      {
          WebClient client = new WebClient ();
          string reply = client.DownloadString (address);
      
          Console.WriteLine (reply);
      }
      

      【讨论】:

        【解决方案5】:

        这是我的答案,一个接受 URL 并返回字符串的方法

        public static string downloadWebPage(string theURL)
            {
                //### download a web page to a string
                WebClient client = new WebClient();
        
                client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
        
                Stream data = client.OpenRead(theURL);
                StreamReader reader = new StreamReader(data);
                string s = reader.ReadToEnd();
                return s;
            }
        

        【讨论】:

        • 最佳答案。谢谢。
        【解决方案6】:

        我认为这是最快(下载速度时间和低延迟)的下载解决方案。

        // WebClient vs HttpClient vs HttpWebRequest vs RestSharp
        // در نهایت به نظرم روش زیر سریعترین روشه
        HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(url);
        Request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
        Request.Proxy = null;
        Request.Method = "GET";
        using (WebResponse Response = Request.GetResponse())
        {
            using (StreamReader Reader = new StreamReader(Response.GetResponseStream()))
            {
                return Reader.ReadToEnd();
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-03-19
          • 1970-01-01
          • 1970-01-01
          • 2016-05-12
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多