Fastest C# Code to Download a Web Page

后端 未结 6 1053
不思量自难忘°
不思量自难忘° 2021-01-30 06:31

Given a URL, what would be the most efficient code to download the contents of that web page? I am only considering the HTML, not associated images, JS and CSS.

6条回答
  •  北荒
    北荒 (楼主)
    2021-01-30 07:05

    I think this is the fastest (download speed time with low latency) solution for download.

    // 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();
        }
    }
    

提交回复
热议问题