How to effectively and stably download files from the Internet

Source: Internet
Author: User

How to effectively and stably download files from the Internet
Author: hangwire

Download the source program of this Article

 How to effectively and stably download files from the Internet is an important issue for many network applications. The code segment provided in this article explores this issue. I hope to help programmers.

 Uint internetgetfile (hinternet in hopen,
Char * szurl,
Char * szfilename,
Hwnd hwndprogress,
Int idstatustext,
Int idprogressbar );

Here, the type of the returned value is uint. If the return value is successful, 0 is returned. Otherwise, a non-zero value is returned. To use this function, you only need to provide a valid hinternet handle, which can be obtained through standard internetopen. If you want to, you can also provide a handle to the Progress window (ID is a static control identifier used to display the status ), declare some variables in the first few lines of code of this function.

 DWORD dwsize;
This variable is used to store how much data is read by calling internetreadfile each time.

 Char szhead [] = "accept: */R/n/R/N ";
Stores multiple HTTP headers. If no header information is transmitted when internetopenurl is called, you can open a text file only!

 Void * sztemp [2, 16384];
Buffer variable, which can store 16 KB of file data from the Internet.

 Hinternet hconnect;
This is an hinternet handle that contains the request result (from internetopenurl)

 File * pfile;
Standard C file handle (must contain stdio. h ). If you want to, you can use the Win32 API to process files.

 If (! (Hconnect = internetopenurla (hopen, szurl, szhead, lstrlena (szhead), internet_flag_dont_cache | internet_flag_pragma_nocache | internet_flag_reload, 0 )))
{
Return internet_error_openurl;
}
 This call can open an Internet file handle using a URL. Indicates that the file is always read, not cached ). If this function fails, an error is returned. You can specify any value of internet_error_openurl. All error messages must be defined for this function. It can also be replaced by a number.

 If (! (Pfile = fopen (szfilename, "WB ")))
{
Return internet_error_fileopen;
}
This call opens a file based on the given file name. If it fails, another user-defined error is returned.

 DWORD dwbytetoread = 0;
DWORD dwsizeofrq = 4;
DWORD dwbytes = 0;
These three values respectively store the file size, the size of httpqueryinfo content, and the total number of bytes read.

 If (! Httpqueryinfo (hconnect, http_query_content_length | http_query_flag_number, (lpvoid) & dwbytetoread, & dwsizeofrq, null ))
{
Dwbytetoread = 0;
}
This call can obtain the file size. If the download fails, dwbytetoread is set to 0, and the percentage and total number are not displayed when the file is downloaded.

 DWORD start;
DWORD end;
DWORD time;
Time = 10;
Start = timegettime ();
To use these bits, you must include mmsystem. h and link winmm. Lib. These bits are used for time selection to indicate the download speed. The sample code only counts the download speed. You can extend this function, for example, estimate the remaining time.

 Do
{
If (! Internetreadfile (hconnect, sztemp, 16384, & dwsize ))
{
Fclose (pfile );
Return internet_error_readfile;
}
In this call loop, a 16 KB data block is downloaded each time. If the download request fails, the file is closed and an error is returned.

 If (! Dwsize)
Break;
Else
Fwrite (sztemp, sizeof (char), dwsize, pfile );
If dwsize is 0, it means an EOF, And the loop exits. Otherwise, the data read by internetreadfile is written to the local file.

 Dwbytes + = dwsize;
If (dwbytetoread & hwndprogress)
{
Senddlgitemmessagea (hwndprogress, idprogressbar, wm_user + 2, (dwbytes * 100)/dwbytetoread, 0 );
Updatewindow (hwndprogress );
}
In this Code, dwbytes is the amount of data read from the file. It increases constantly. If the file length is valid, the Progress window handle is specified, and the progress bar is updated to indicate the download progress.

 Float fspeed = 0;
Fspeed = (float) dwbytes;
Fspeed/= (float) Time)/1000.0f;
Fspeed/= 1024366f;
These bit codes are used to calculate the download speed and data size based on the time spent.

 If (hwndprogress)
{
Char s [260];
Sprintf (S, "% d KB/% d kb @ % 1.1f kb/s", dwbytes/1024, dwbytetoread/1024, fspeed );
Setdlgitemtexta (hwndprogress, idstatustext, S );
Updatewindow (hwndprogress );
}
Sets and processes the status text in the Progress window, indicating the size and speed of the downloaded file.

 End = timegettime ();
Time = end-start;
If (time = 0)
Time = 10;

Time updated

} // Do
While (true );

Loop ends
Fflush (pfile );
Fclose (pfile );
Return 0;
}
Finally, the function ends, closes the file, and clears the buffer of the hardware driver. If the return value is 0, the operation is successful.

 Using this code snippet, as described in this article, you can write a program to effectively and stably download files from the Internet. See examples for implementation details.

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.