I'm trying to download the HTML source code in the same form as is shown when right-clicking and selecting the option on a website. So far I tried:
using System.Net;
using (WebClient client = new WebClient ()) // WebClient class inherits IDisposable
{
client.DownloadFile("http://yoursite.com/page.html", @"C:\localfile.html");
// Or you can get the file content without saving it
string htmlCode = client.DownloadString("http://yoursite.com/page.html");
}
and
string url = "page url";
HttpClient client = new HttpClient();
using (HttpResponseMessage response = client.GetAsync(url).Result)
{
using (HttpContent content = response.Content)
{
string result = content.ReadAsStringAsync().Result;
}
}
as suggested here.
The issues I am facing is that the source code in the first version did not contain the information I needed (which is available in the Chrome source code version). I read something about that what I am actually downloading is DOM, meaning some links and content may be rewritten unless I have misunderstood.
While trying the second method I received a System.AggregateException at string result = content.ReadAsStringAsync().Result;
, so even if that solution would have worked I do not know how to debug it.
The plan now is to read up on the HttpClient class and meanwhile see if someone here knows the solution right away. I followed the same syntax as shown in an answer in the link and the compiler did not react to any typos. The only difference was that I pasted an actual URL.