Streaming Data on the Net

We have already know how to use stream and serialization. Now, we could apply this knowledge to send and receive data on the internet.

In this subchapter, we'll be using a couple of web API from Microsoft Cognitive Services.

First, we want to try to get the data of google.com. We start with creating a new function called GetGoogleHomePage.

public static string GetGoogleHomePage()
{
    var webClient = new WebClient();
    byte[] googleHome = webClient.DownloadData("https://www.google.com");

}

Then, we use streamreader to get the googlehomepage elements.

public static string GetGoogleHomePage()
{
    var webClient = new WebClient();
    byte[] googleHome = webClient.DownloadData("https://www.google.com");

    using (var stream = new MemoryStream(googleHome))
    using (var reader = new StreamReader(stream))
    {
        return reader.ReadToEnd();
    }
}

Now, we call the function in Main method.

Console.WriteLine(GetGoogleHomePage());

results matching ""

    No results matching ""