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