HTTP download using C ++ and wininet

Source: Internet
Author: User

Recently, wxWidgets was used to build a project on the win platform, involving downloading files from the HTTP server. wxhttp found that it would take more than 10 seconds to download a 100-kb file over the Intranet, in turn, I want to use the network library in boost, but considering the need to increase the size of the program EXE, I plan to use socket to encapsulate one, and I have no intention of rolling it to wininet on msdn, so I will try again!

Follow these steps to use wininet for HTTP download:

1. preparations. Before using the wininet function, call the internetattemptconnect function to check whether the network connection exists. Use the internetcheckconnection function to ping the server.
2. initialize wininet. Use the internetopen function to establish a network connection environment. This function is not connected to the network to verify the correctness of the Set environment. This function returns an environment handle hinternet.
3. Use the internetconnect function to establish a session in a network connection environment. A session includes site information and connection parameters. For the HTTP protocol, this function does not manage and establish a connection to the site. This function returns a session handle hinternet.
4. Use httpopenrequest to enable a request, use httpaddrequestheaders to add additional request attributes, and use httpsendrequest to send requests. A connection is established only when httpsendrequest is used. For download, only get requests are required. Therefore, the specific request format or content is not discussed. For details, refer to the HTTP standard.
5. recursively query the length of the currently downloaded content using the internetquerydataavailable function, and use the internetreadfile function to read the downloaded content. Here, only blocking download is used. wininet HTTP also supports asynchronous download.
6. the download is complete. Close the handles.

Close!

Source code:

Http://download.csdn.net/detail/revofu/5003976

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.