-->

Saving a webpage to disk using C++

2019-01-29 10:12发布

问题:

I've managed to download a "file" from the internet with the help of wininet library, but I can't seem to save a "webpage" i.e. something I can edit later on with a text editor or with ifstream.

In this case, what are the tools I should resort to? Can wininet save a webpage to disk? Should I consider cURL (though I haven't managed to download regular files due to lack of documentation of cURL)? Do I need to learn what's called socket programming?

NB: I'm on Windows, using MinGW but can switch to MSVC if necessary, I'm looking for source code in the webpage, eventually I'm after the text in a webpage. Also, I am not familiar with any of the functions in wininet, curl, or sockets. What do I need to learn of these?

Any help is greatly appreciated!

回答1:

If your program is going to run both on windows and unix, then use cURL. Otherwise, stick with MSVC and WinINet functions http://msdn.microsoft.com/en-us/library/windows/desktop/aa385473(v=vs.85).aspx It's much easier to use in terms of the efforts required to get your program running and distributed (esp. if you're not linking your program against cUrl statically. Otherwise, you'll need to take libcurl.dll everywhere your program runs on Windows). With WinINet, you simply need to include a header and a library to use the functions.

If you're going to use WinINet, refer to this code snippet: http://www.programmershelp.co.uk/showcode.php?e=57 Use the same code except for the while loop. Instead of reading one byte at a time, read them by chunks and write them to the output file handle.

If you're going to use cURL, refer to this post: Download file using libcurl in C/C++



标签: c++ curl wininet