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



我写了以下代码,从给定的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();
   }

但是下载HTML文件大约需要5-8秒,这非常慢。我的问题是,有没有办法改进这段代码,或者使用其他一些代码/库来执行给定 URL 的 HTML 下载速度比这个代码/库更快?

有人可以帮助我吗?

为什么不使用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
                }
            }
        }
    }
}

相关内容

  • 没有找到相关文章

最新更新