我已经编写了以下代码,可以从给定的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();
}
但这需要5-8秒来下载HTML文件,这是相当慢的。我的问题是,有没有办法改进这段代码,或者使用其他一些代码/库,可以比这个更快地执行给定URL的HTML下载?
有人能帮帮我吗?
发布于 2017-03-20 21:30:53
为什么不使用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
}
}
}
}
}
https://stackoverflow.com/questions/42903215
复制相似问题