简体   繁体   English

用C#下载HTML页面并将其以最快的方式加载到内存中

[英]Downloading HTML page in C# and loading it in memory fastest way

I have written a following code which downloads a page from a given URL: 我编写了以下代码,该代码从给定的URL下载页面:

   string html = string.Empty;
   HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
   request.AutomaticDecompression = DecompressionMethods.GZip;
   request.Proxy = null;
   request.ServicePoint.UseNagleAlgorithm = false;
   request.ServicePoint.Expect100Continue = false;
   request.Method = "GET";
   using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
   using (Stream stream = response.GetResponseStream())
   using (StreamReader reader = new StreamReader(stream))
   {
   html = reader.ReadToEnd();
   }

But this takes about 5-8 seconds to download the HTML file which is quite quite slow. 但是下载HTML文件大约需要5到8秒的时间,这相当慢。 My question here is, is there any way to improve this code, or use some other piece of code/library that can perform the HTML download for a given URL faster than this one? 我的问题是,是否有任何方法可以改进此代码,或者使用其他代码/库可以比给定的URL更快地执行给定URL的HTML下载?

Can someone help me out ? 有人可以帮我吗 ?

Why not use an httpclient then write the result to a file that way? 为什么不使用httpclient然后将结果以这种方式写入文件?

using (HttpClient client = new HttpClient())
{
    using (HttpRequestMessage request = new HttpRequestMessage())
    {
        request.Method = HttpMethod.Get;
        request.RequestUri = new Uri("http://www.google.com", UriKind.Absolute);

        using (HttpResponseMessage response = await client.SendAsync(request))
        {
            if (response.IsSuccessStatusCode)
            {
                if (response.Content != null)
                {
                    var result = await response.Content.ReadAsStringAsync();
                    // write result to file
                }
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM