Internet File Download

Source: Internet
Author: User

Use internetgetconnectedstate () to check the local network status
2 hopen = internetopena () Open the connection to the Internet
3. Call hconnect = internetopenurla () to open an Internet file handle using a URL.
4. Open a local file
5. Size of the Internet File obtained by calling httpqueryinfo ()
6. Call internetreadfile () to download the data block.
7. Write files
8 close files
9 internetclosehandle (hopen); Disable Internet connection

# Include <wininet. h>
# Include <mmsystem. h>

Int cinternetdownloadxdlg: internertdownload (void * pdata)
{
 
DWORD dwflags;
Internetgetconnectedstate (& dwflags, 0); // retrieves the connected state of the Local System
Char stragent [64];
Sprintf (stragent, "Agent % lD", timegettime ());
Hinternet hopen;
If (! (Dwflags & internet_connection_proxy ))
{
// Open the connection to the Internet
Hopen = internetopena (stragent, // Application name
Internet_open_type_preconfig_with_no_autoproxy, // access type parameter
Null,
Null,
0 );
}
Else
{
Hopen = internetopena (stragent, internet_open_type_preconfig, null, null, 0 );
}

If (! Hopen)
{
Afxmessagebox ("Internet connection error! ");
Return-1;
}
DWORD dwsize; // This variable is used to store the data read by each call to internetreadfile.
Char szhead [] = "accept: */R/n/R/N"; // stores multiple HTTP header information. If no header information is transmitted when internetopenurl is called, you can only open a text file.
Void * sztemp [16384]; // buffer variable, which can store 16 KB of file data from the Internet
Hinternet hconnect; // This is an hinternet handle containing the request result (from internetopenurl)
Cfile file;

If (! (Hconnect = internetopenurla (hopen, m_url, szhead,
Lstrlena (szhead), internet_flag_dont_cache | internet_flag_pragma_nocache |
Internet_flag_reload, 0) // This call can open an Internet file handle using a URL. Indicates that this file is always read, not cached
{
Afxmessagebox ("this URL cannot be opened! ");
Return-1;
}

If (file. Open (m_dirfile, cfile: modewrite | cfile: modecreate) = false)
{
Afxmessagebox ("the local file cannot be opened! ");
Return-1;
}

DWORD dwbytetoread = 0; // size of the stored File
DWORD dwsizeofrq = 4; // The content size of httpqueryinfo
DWORD dwbytes = 0; // total number of bytes read

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

Do
{
// Download a 16 KB data block each time
If (! Internetreadfile (hconnect, sztemp, 16384, & dwsize ))
{
Afxmessagebox ("An error occurred while reading the file! ");
File. Close ();
Return-1;
}

If (dwsize = 0)
Break;
Else
File. Write (sztemp, dwsize );
Dwbytes + = dwsize;
 
} While (true );
File. Close ();
Internetclosehandle (hopen );
Return 0;
}

 

 

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.