Resumable download of HTTP files in Windows winnet

Source: Internet
Author: User

1. Download the MFC File

There are two methods:
A)
Create a cinternetsession object-> call cinternetsession: Openurl this function parses the URL, opens a connection with the server specified by the URL, and returns a read-only cinternetfile object-> cinternetfile :: read the file and analyze the cinternetsession

B)
Create a cinternetsession object-> call cinternetsession: gethttpconnection
Return chttpconnection object-> call chttpconnection: openrequest to create a chttpfile object-> chttpfile: sendrequest to send a connection request-> chttpfile: queryinfo to obtain HTTP information (such as file size and etag) | cinternetfile: Read File-> analysis structure cinternetsession

In the above process, if an error occurs, a cinternetexception exception is thrown.
In addition, you can make the following settings before opening the link.
Cinternetsession: setoption to read or set the internetquery option (for example, timeout or number of retries)
Cinternetsession: enablestatuscallback: sets the callback function to monitor the session status.
Chttpfile: addrequestheaders set the HTTP Request Header (called before chttpfile: sendrequest)

2. resumable upload
Resumable upload is actually implemented by setting the file range to be downloaded in the HTTP request header. A typical HTTP request header is

Gethttp: // class/download.microtool.de: 80/somedata.exe
HOST: download.microtool.de
Accept :*/*
Pragma: No-Cache
Cache-control: No-Cache
Referer: http: // class/download.microtool.de/
User-Agent: Mozilla/4.04 [En] (Win95; I; NAV)
Value Range: bytes = 554554-
 
Note that the last line: range: bytes = 554554-is in the format of range: bytes = start position-end position. In other words, you can set the start and end position of the HTTP request header, to obtain a part of the HTTP file.

3. Win32 wininet API implementation
Similar to the MFC function, the following functions are generally required for downloading:

Internetopen is the first function called. It returns an hinternet handle, which is defined as a hsession, that is, a session handle, which is equivalent to a cinternetsession.
Internetconnect uses the hsession handle. The returned HTTP connection handle is hconnect, which is equivalent to cinternetsession: gethttpconnection.
Httpopenrequest uses the hconnect handle. The returned handle is an HTTP request handle defined as hrequest, which is equivalent to chttpconnection: openrequest.
Httpsendrequest (equivalent to chttpfile: sendrequest), httpqueryinfo, internetsetfilepointer, and internetreadfile both use the handle returned by httpopenrequest, that is, hrequest.

Cinternetsession: Openurl implements three functions: internetconnect & httpopenrequest & httpsendrequest.

When these handles are no longer used, use the internetclosehandle function to close them to release the resources they occupy.

using wininet to develop Internet client application guide (I) http://www.vckbase.com/document/viewdoc? Id = 545
Use wininet to develop Internet client application guide (2) http://www.vckbase.com/document/viewdoc? Id = 546
using cinternetsession to encapsulate multithreading Http File Download http://www.vckbase.com/document/viewdoc? Id = 1693
resumable HTTP download http://sunyan331.spaces.live.com/blog/cns! 89b9f8bf2575e281! 947. entry
HTTP server breakpoint download files (which has a good source code) http://www.cnitblog.com/wangk/archive/2007/05/22/5942.html
write breakpoint resume and multi-thread download (with source code) http://www.bbbh.org/20060427/2620/
wininet: implementing resuming feature http://www.clevercomponents.com/articles/article015/resuming.asp
(a very detailed Code explanation) retrieving a file. HTTP http://www.codeproject.com/KB/IP/getwebfile.aspx

Related Article

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.