C++ Qt 64 - QHttp download files from the web with ease

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
everybody its Brian and welcome to the 64th cute tutorial with C++ and GUI programming today we're going to be actually downloading some stuff off the web using cute we're going to cover the cue HTTP class so just go into the help system type Q HTTP and just browse over this class real quick if you wonder what 8t T P stands for its hypertext Transfer Protocol that's how it's how files are moved around the web for example if you go to a web page here's my website you notice the HTTP in front of this it means we're using the hypertext Transfer Protocol part of this protocol is you make a request using it and when I say using it you're actually sending a packet saying get this file the web server will turn around see if the file exists if it does it sends back the file in this case you know looking at this it's sent back the web page so you can see there's the contents of the web page that we downloaded and it downloads you know all the images and stuff - and if it doesn't exist it sends back an error code like we'll just say index.html and you see how it says the dreaded 404 not found will 404 is status code that's an HTTP status code so what we're going to actually do today is we're going to make a program to download files and so we're going to say new and we're just going to say project console application and call do HTTP and eat test next next finish finish and we've got our real basic application here now the first thing you want to do is actually add a reference into the network module oops help if I could spell Network and once you've added a reference to the network module it'll rescan your project and then life goes on you can do what you need to do so we will add a new class and we'll just call this downloader my my computer's been acting sorry you want to inherit cue object well think about it my computer has been acting a little little funky here I installed Windows 7 sp1 and I've been having all sorts of weird issues like it blue screen down me 20 minutes ago and it hasn't done that in about 10 years so kind of concerned that maybe Microsoft didn't get their stuff right this time alright now we want to include queue HTTP we also want to include queue file and let's of course include cutie bug help if I get the pound sign in there still three now that we've got those in there we need to make a function that we're going to actually kick this whole process off so safe would do download and of course we need the the object we're actually going to to work with here so we'll say Q HTTP and we'll just call it HTTP that way we know what it is and part of working with the QH TTP class is it will while it's downloading fire off some signals here here's the signals we got to work with you know there's a whole bunch of them and I'm not going to get into all of them but the ones we're really focused on our state changed response header received and request finished and what these are is the state changed let's just click on that there's different states of the connection unconnected host looked up connecting sending receiving if you watch the network primer that I did last night you'll really understand what I'm talking about this uses a TCP connection so it's got the underlying three-way handshake and the state change signal tells you wearing that and we're in that handshake you are and what the class is actually doing under the hood the response header received is part of the HTTP protocol when you request a file it sends you the file but in front of it it sends a header that has metadata about the file and the transaction that you're making so the first packet always has this header so that's when you get the header and request finished is when the actual request is finished and you have all of the file I'm not 100% certain but I'm sure some of you gurus out there going to correct me if I'm wrong I believe the cue HTTP class cashes it into a temp file somewhere and then you can pull it out wherever you want so without further ado let's actually let's flip back here and implement these we're just going to copy that paste it and then we want the response header received copy that and then we want ready read whoop sorry not ready read we want request finished um you might be asking yourself what's the difference train ready reading request finished well ready read is fired whenever you have data in the buffer that can be read request finish is fired when the entire process is done so if you try doing this off ready read you may not have all of the file you may only have a chunk of it because remember you have a certain window which you can send packets in so if you have exceeded that window let's just think of it as a bucket I'll say you got a one gallon bucket and you're trying to transfer five gallons of water what's going to take five buckets or five packets to get this through now I don't want to scare with the details you don't really need to know any of that to use this class you just need to understand the concept of how that works all right so now that we've got our function prototypes here let's go in here and actually implement these and we'll save Lloyd downloader do download and that is going to actually be where we're going to kick this whole process off here and we're just going to implement these really quickly all right first thing we're going to want to do now that we've got them implement is we're going to want to flesh out these slots so that way we can see exactly what's going on here so let's do the state change first so we'll say switch state and I'm just gonna do this very quickly case zero okay I say quickly and then I goof up okay case 0 break and I'm looking at my notes but you may have to flip back over into here and actually go state changed and state that way you can see which state is which value and 0 is unconnected and there are seven states that we need to worry about here state 1 is host lookup it's actually uppercase that just because I'm picky state two is connecting and really you don't need to worry about the states I'm just doing this for illustrative purposes just so you can see you know what's going on under the hood I really want you to understand that there's a multi-part process you don't just fire something out in the server returns data I mean there's a whole lot that happens in the background here five is connect and six of course is closing all right now our response how to receive remember this is going to be the first packet if we're transferring a file it takes five buckets this will be the first bucket we're gonna get the response header now remember we don't actually have any real data yet we're just getting the header or the information about the data we're going to get say cute a bug and we want the size so let's just grab size let's grab our reference to the queue response header and we'll say content length and through the magic of copy and paste we're going to add a few more things here we want the type because we want to know whether we're grabbing you know a text file or an image or anything like that and we want to know the status code HTTP status code remember when I went to the web page I typed in a file it didn't exist well that's the status code just for information purposes if it says 200 that means the the whole thing is okay it literally means 200 okay anything in the 400s means there's a problem with your request and 500 means there's a problem with the server okay now we've got our request finished and notice how there's this full error let's say you tell if those err in the whole process so what you can say is if air and else and we'll just do Q debug we say error that way we know something bad happened otherwise we say Q debug okay that way we know you know the request is finished life is good everything goes on here now this is fine and dandy but we haven't actually told it to download anything yet so what we need to do is actually create our object so HTTP a equal new queue HTTP say this is the parent class and now we want to connect up our signals and slots we got state changed and then this with slot oops we want the response header received and finally we will connect the HTTP signal and what did we have here we had the request finished now that we've got our signals and slots implemented the final step of this is we need to actually request a file so we say HTTP set host that way you're setting the server and you can set it to whatever you want but I'm just going to do my server void realms just because I'm used to my server and I know how it behaves and then we want to actually get a file and we're just going to get the root of that website or the slash and what this will do when if we go up to the website is it will say okay get the base base directory here and if there's a default file the web server will serve the default file you see there's our slash right here at the end so what the web server does and says okay go to the root of the website looks for the default file which in my website's case is called default hopefully it spelled default.aspx so if you go to slash default.aspx the same thing and you should note before we fire off this tour if we go index.html it's going to return 404 not found so let's save our work and really quickly let's call out our class here so sit down downloader C down do download compile and run it with any luck we don't have any errors in our code here and wet faster then I could drag it over but you can see how it says connecting sending reading the size that's the size of the information they type the mime type is text slash HTML it means is it's text file the status is 200 number 200 is good and connect means we're connected now you notice how it didn't disconnect part of the HTTP protocol is a keepalive parameter and what that'll do is if there's no problems with your request it'll just keep that connection open for as long as you need it that way you can make multiple requests off the same connection because making a connection making that three-way TCP handshake is actually rather expensive and slow so they just keep the connection open so you just keep making requests off the same connection now let's show you what happens if we request a file that it doesn't exist remember index.html does not exist off my website so we'll compile run and you see we get different results here we get connecting sending reading for 4044 the size type a text out HTML status 404 remember that's the not found and then notice closing unconnected meaning the webserver just said goodbye I'm done with you so that's really all you need to know about these connections now before I close this tutorial I know somebody out there is going to ask me Brian how do you download this to a file well I'm kind of glad you asked in the request finish we could have okay but we can also just dump this into a file and I've got a directory out here there's absolutely nothing in it and we're going to download it to that directory so I'm gonna do is say queue file let's say file we'll new flips better make this pointer and we want to give this a name here I'm going to throw my name a test downloads but you can put it wherever you want remember to change your slashes to the universal slash otherwise it'll read it as an escape character and we'll say test dot txt and then we will say if file open and we want the mode which we're just going to say it pend then we'll delete the file that way we don't have a memory leak but if it's open then we want to actually write that information to the desk so we're going to say file right and we want HTTP read all and what that will do is that will give us a cube byte array and we can take that cube byte array and we can just throw it right into the QIO device the cue file and that will write everything out to disk and now it will say file we want to flush the contents of this make sure everything gets written to disk and file close there we go now you should also note instead of doing this off the request finish you can actually hand it in the get as a parameter you can hand it a cue i/o device so you could just make a cue file in memory and put a reference to the cue file pointer right here that way as it's getting the information it automatically writes it off to disk for you so you don't have to worry about the i/o and then when the request is finished you would just you know of course flush closed and delete your file but I'm trying to keep this tutorial really simple and very easy to understand so let's save our work pile and run and you can see how it's for 404 error you know file not file and closing unconnected but if we go out to our download directory you see we now have this test txt and if we open this up you see there's the contents and this is the actual information that's displayed on the web page you can see it actually says you know HTTP 404 and it says you know the page cannot be found so that's what this is right here and you can actually prove that by going you know right click view source and that's what we just downloaded right there so anyways if I need to cut this tutorial off because uh running out of time and well you know how it is with YouTube so thank you for watching I hope you found this video entertaining and please you know shoot me any questions you guys got I enjoy your feedback it really keeps me on my toes and you know keeps me honest because sometimes I say things and think I know what I'm talking about and I really tell it so just definitely keep up on that feedback
Info
Channel: VoidRealms
Views: 54,047
Rating: undefined out of 5
Keywords: Qt, 64, QHttp, download, files, from, the, web, with, ease
Id: dw50MnqH-Qo
Channel Id: undefined
Length: 19min 10sec (1150 seconds)
Published: Thu Mar 24 2011
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.