1 00:00:00,330 --> 00:00:04,280 The .NET framework has a couple of ways we can send and receive data but 2 00:00:04,280 --> 00:00:06,740 we're going to use a simple one, web client. 3 00:00:07,800 --> 00:00:09,620 I've included links in the notes for 4 00:00:09,620 --> 00:00:12,840 other ways to make web requests that include some more options. 5 00:00:12,840 --> 00:00:17,300 We'll do a quick example of using web client to download the Google home page 6 00:00:17,300 --> 00:00:17,940 HTML body. 7 00:00:18,950 --> 00:00:22,710 In our program class,we'll create a new method to do the work. 8 00:00:22,710 --> 00:00:23,585 Clean this up a little bit. 9 00:00:27,268 --> 00:00:33,920 Okay, public static string GetGoogleHomePage, 10 00:00:33,920 --> 00:00:38,870 we'll need to use the webClient class 11 00:00:38,870 --> 00:00:44,800 which is in the system.net namespace. 12 00:00:44,800 --> 00:00:50,608 We'll use our quick action trick so that Visual Studio will add it for 13 00:00:50,608 --> 00:00:53,823 us, var webClient = new WebClient. 14 00:00:55,040 --> 00:00:56,740 Remember the shortcut key? 15 00:00:56,740 --> 00:01:00,276 It's a shortcut that I recommend you memorize because it's really useful, 16 00:01:00,276 --> 00:01:07,520 Control+Period and using System.NET. 17 00:01:07,520 --> 00:01:11,560 Bam we've got a namespace, let's see what methods we've got on our web client 18 00:01:11,560 --> 00:01:21,000 object, webClient.DownloadData, 19 00:01:21,000 --> 00:01:22,760 that sounds like something we could use. 20 00:01:23,790 --> 00:01:28,410 It returns a byte array, we'll need to convert the byte array to a stream and 21 00:01:28,410 --> 00:01:30,360 then use a StreamReader to read it for us. 22 00:01:31,860 --> 00:01:36,510 First let's create a variable to hold our byte array, byte 23 00:01:36,510 --> 00:01:43,170 googleHome = and 24 00:01:43,170 --> 00:01:46,970 our download data method takes a string address. 25 00:01:46,970 --> 00:01:55,800 So we can just type in https:// www.google.com. 26 00:01:55,800 --> 00:02:01,740 Now that we've got our byte array, we need a stream to put it in. 27 00:02:01,740 --> 00:02:05,860 We haven't actually used a stream directly yet because the version of StreamReader we 28 00:02:05,860 --> 00:02:09,674 used took a file and created the stream for us behind the scenes. 29 00:02:09,674 --> 00:02:16,277 Stream stream = new Stream. 30 00:02:19,510 --> 00:02:21,610 We can't instantiate a Stream object, 31 00:02:21,610 --> 00:02:24,700 because the Stream class itself is abstract. 32 00:02:24,700 --> 00:02:28,360 Abstract classes are kind of like interfaces, but not. 33 00:02:28,360 --> 00:02:31,030 They can't be instantiated and must be inherited from. 34 00:02:32,060 --> 00:02:35,340 Unlike interfaces they can have some implementation and 35 00:02:35,340 --> 00:02:39,020 a class can only inherit from one abstract class. 36 00:02:39,020 --> 00:02:43,210 I've included a little more information about abstract classes in the notes. 37 00:02:43,210 --> 00:02:47,070 Let's go check out the docs and see if there's something else we can use. 38 00:02:47,070 --> 00:02:49,080 I'll click on Stream and hit F1. 39 00:02:50,480 --> 00:02:56,330 Okay, here's all the different classes that inherit from Stream, BufferedStream, 40 00:02:56,330 --> 00:03:01,170 FileStream, our byte array is already in memory. 41 00:03:01,170 --> 00:03:03,315 So let's see what this is, MemoryStream. 42 00:03:07,960 --> 00:03:10,780 Creates a stream whose backing store is memory. 43 00:03:10,780 --> 00:03:12,160 Let's take a look at the constructors. 44 00:03:15,590 --> 00:03:18,930 Ok, we can create an instance of a memory stream with a byte array. 45 00:03:19,980 --> 00:03:25,380 Let's go back to our code, so here we can say, 46 00:03:25,380 --> 00:03:32,442 don't forget our using(var stream = new MemoryStream and 47 00:03:32,442 --> 00:03:37,710 we'll pass it our array of (googleHome). 48 00:03:40,400 --> 00:03:45,418 So now that we have our stream we need a stream reader using (var 49 00:03:45,418 --> 00:03:51,780 reader = new StreamReader and we'll pass it the stream. 50 00:03:54,470 --> 00:03:56,811 And now we need our curly braces, 51 00:03:56,811 --> 00:04:01,081 on the reader object we can just return reader.ReadToEnd. 52 00:04:04,570 --> 00:04:09,291 Return, I want to call out why I'm using the var keyword here instead of 53 00:04:09,291 --> 00:04:12,945 declaring the variable with the class name. 54 00:04:12,945 --> 00:04:17,375 We can technically use the var keyword for all these variables because the compiler 55 00:04:17,375 --> 00:04:21,185 can interpret the type by what follows the assignment operator. 56 00:04:22,450 --> 00:04:26,940 So here where we type far reader, the compiler knows that it will 57 00:04:26,940 --> 00:04:30,800 be a stream reader because we're assigning it to a new stream reader. 58 00:04:31,950 --> 00:04:35,180 I like to use the var keyword because it's much faster but 59 00:04:35,180 --> 00:04:38,770 I only like to use it when it's very obvious what the type is. 60 00:04:38,770 --> 00:04:43,380 So up here with our googleHome variable, I used a byte array to declare 61 00:04:43,380 --> 00:04:47,780 the type instead of using var, because even though the compiler can figure out 62 00:04:47,780 --> 00:04:51,940 what it would be, it's easier for someone else who's reading this code 63 00:04:51,940 --> 00:04:56,260 to know the variable type without having to hover over the download data method. 64 00:04:57,790 --> 00:05:02,130 Back up in Main, let's call our GetGoogleHomepage method and 65 00:05:02,130 --> 00:05:03,655 print out the result to the console, 66 00:05:03,655 --> 00:05:13,357 Console.WriteLine(GetGoogleHomePage). 67 00:05:13,357 --> 00:05:17,300 And I'll comment out all this stuff up here for now 68 00:05:19,470 --> 00:05:24,850 by holding down the CTRL and pressing K then C. 69 00:05:26,880 --> 00:05:28,086 I forgot this line here too. 70 00:05:31,587 --> 00:05:33,916 Now let's run it with CTRL+F5. 71 00:05:35,918 --> 00:05:40,303 Well then, there's the home page body for Google, a browser would take that and 72 00:05:40,303 --> 00:05:41,410 render us the page.