WebClient5:42 with Carling Kirk
We'll use the WebClient class to download the Google home page.
The .NET framework has a couple of ways we can send and receive data but 0:00 we're going to use a simple one, web client. 0:04 I've included links in the notes for 0:07 other ways to make web requests that include some more options. 0:09 We'll do a quick example of using web client to download the Google home page 0:12 HTML body. 0:17 In our program class,we'll create a new method to do the work. 0:18 Clean this up a little bit. 0:22 Okay, public static string GetGoogleHomePage, 0:27 we'll need to use the webClient class 0:33 which is in the system.net namespace. 0:38 We'll use our quick action trick so that Visual Studio will add it for 0:44 us, var webClient = new WebClient. 0:50 Remember the shortcut key? 0:55 It's a shortcut that I recommend you memorize because it's really useful, 0:56 Control+Period and using System.NET. 1:00 Bam we've got a namespace, let's see what methods we've got on our web client 1:07 object, webClient.DownloadData, 1:11 that sounds like something we could use. 1:21 It returns a byte array, we'll need to convert the byte array to a stream and 1:23 then use a StreamReader to read it for us. 1:28 First let's create a variable to hold our byte array, byte 1:31 googleHome = and 1:36 our download data method takes a string address. 1:43 So we can just type in https:// www.google.com. 1:46 Now that we've got our byte array, we need a stream to put it in. 1:55 We haven't actually used a stream directly yet because the version of StreamReader we 2:01 used took a file and created the stream for us behind the scenes. 2:05 Stream stream = new Stream. 2:09 We can't instantiate a Stream object, 2:19 because the Stream class itself is abstract. 2:21 Abstract classes are kind of like interfaces, but not. 2:24 They can't be instantiated and must be inherited from. 2:28 Unlike interfaces they can have some implementation and 2:32 a class can only inherit from one abstract class. 2:35 I've included a little more information about abstract classes in the notes. 2:39 Let's go check out the docs and see if there's something else we can use. 2:43 I'll click on Stream and hit F1. 2:47 Okay, here's all the different classes that inherit from Stream, BufferedStream, 2:50 FileStream, our byte array is already in memory. 2:56 So let's see what this is, MemoryStream. 3:01 Creates a stream whose backing store is memory. 3:07 Let's take a look at the constructors. 3:10 Ok, we can create an instance of a memory stream with a byte array. 3:15 Let's go back to our code, so here we can say, 3:19 don't forget our using(var stream = new MemoryStream and 3:25 we'll pass it our array of (googleHome). 3:32 So now that we have our stream we need a stream reader using (var 3:40 reader = new StreamReader and we'll pass it the stream. 3:45 And now we need our curly braces, 3:54 on the reader object we can just return reader.ReadToEnd. 3:56 Return, I want to call out why I'm using the var keyword here instead of 4:04 declaring the variable with the class name. 4:09 We can technically use the var keyword for all these variables because the compiler 4:12 can interpret the type by what follows the assignment operator. 4:17 So here where we type far reader, the compiler knows that it will 4:22 be a stream reader because we're assigning it to a new stream reader. 4:26 I like to use the var keyword because it's much faster but 4:31 I only like to use it when it's very obvious what the type is. 4:35 So up here with our googleHome variable, I used a byte array to declare 4:38 the type instead of using var, because even though the compiler can figure out 4:43 what it would be, it's easier for someone else who's reading this code 4:47 to know the variable type without having to hover over the download data method. 4:51 Back up in Main, let's call our GetGoogleHomepage method and 4:57 print out the result to the console, 5:02 Console.WriteLine(GetGoogleHomePage). 5:03 And I'll comment out all this stuff up here for now 5:13 by holding down the CTRL and pressing K then C. 5:19 I forgot this line here too. 5:26 Now let's run it with CTRL+F5. 5:31 Well then, there's the home page body for Google, a browser would take that and 5:35 render us the page. 5:40
You need to sign up for Treehouse in order to download course files.Sign up