Reading HTML pages with . NET Core

Asked

Viewed 331 times

4

Next, I have a code that performs reading of a particular web page:

private string GetCodePage()
{
    WebRequest request = WebRequest.Create(URL);
    WebResponse response = request.GetResponse();
    StreamReader reader = new StreamReader(response.GetResponseStream());

    var codePage = reader.ReadToEnd();

    reader.Close();
    response.Close();

    return codePage;
}

I am trying to make the same code with . NET Core, but the classes have changed, for example, there is no longer the method WebRequest.GetResponse.

Does anyone know how to read html pages by Dotnet Core?

  • What do you mean by "reading" web page? Save page source on disk?

  • I have a robot that obtains information through the source of the pages. If the solution is to download the source, it is no problem, because I can read as a file.

2 answers

4

You’ll have to use the new class HttpClient in System.Net.Http, that is available no Nuget.

A very simple example of how to use:

using(var client = new HttpClient())
{
    var response = await client.GetStringAsync(URL);
    //Existem outras opções aliém do GetStringAsync, aí você precisa explorar a classe
}

1

I managed to solve with the following code:

    private string GetCodePage()
    {
        HttpClient httpClient = new HttpClient();
        var response = httpClient.GetAsync(URL).Result;
        return response.Content.ReadAsStringAsync().Result;
    }
  • Beware of classes that implement IDisposable. You may have trouble ignoring the Dispose of HttpClient.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.