简体   繁体   中英

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:

   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. 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?

Can someone help me out ?

Why not use an httpclient then write the result to a file that way?

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
                }
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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