Reputation: 3825
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 ?
Upvotes: 1
Views: 1115
Reputation: 778
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
}
}
}
}
}
Upvotes: 1