Delphi uses wininet for download

Source: Internet
Author: User
Wininetdownload ('HTTP: // download place name', 'c: \ local filename ')

Reference

Unit xhttpgetthread; interfaceconst maxblocksize = 1024; function wininetdownload (URL: pchar; filename: string): Boolean; implementationuses windows, classes, wininet, sysutils; function wininetdownload (URL: pchar; filename: string): Boolean; var hsession: hinternet; hopenurl: hinternet; temp: array [0 .. MAXBLOCKSIZE-1] of byte; dwread: DWORD; FS: tfilestream; dwresult: DWORD; begin result: = false; hses Sion: = internetopen ('rookie/100', // specifies the application or entry to call the wininet function. This entry is used as a user proxy in HTTP. Internet_open_type_preconfig, // access request type. This parameter can be one of the following values: {internet_open_type_direct parses all local hosts; internet_open_type_preconfig returns the proxy or direct configuration in the registry; internet_open_type_preconfig_with_no_autoproxy returns proxy or direct configuration in the registry, and prevents the use of Microsoft JScript or INS files. internet_open_type_proxy transmits requests as proxies, unless the proxy provides a bypass list and the Resolved Name can bypass the proxy, the function uses internet_open_type_direct .} Nil, // specifies the proxy server name when the laccesstype type is internet_open_type_proxy. Nil, // points to a string that specifies an optional host name list or IP address. The list can contain unknown elements. 0); {This parameter can be any combination of the following values: internet_flag_async can only be used for asynchronous requests acting on the subhandle of the handle returned by the function. Internet_flag_from_cache does not make network requests. All objects are returned by the cache. If the request entry is not cached, an appropriate error is returned. Internet_flag_offline is the same as internet_flag_from_cache .} // Success: a valid handle is returned, which is passed by the application to the next wininet function. // Failed: NULL is returned. If hsession = nil then exit; // open a resource through a complete FTP, Gopher, or http url. Hopenurl: = internetopenurl (hsession, // The current internet session handle. The handle must be returned by the previous internetopen call. URL, // a pointer to the string variable ending with a null character, specifying the URL to be read. Only URLs starting with ftp:, gopher:, http:, or https: are supported. Nil, // a pointer to a string variable ending with a null character, specifying the header information sent to the HTTP server. For more information, see the description of the lpszheaders parameter in the httpsendrequest function. 0, // the size of the extra header, in tchar. If this parameter is-1l and lpszheaders is not null, lpszheaders is assumed to be zero-terminated (asciiz), and the length is automatically calculated. 0, // internet_flag_dont_cache, // No entity returned to the cache is added to {internet_flag_existing_connect. If you use the same required attribute to create a session, you will try to use the existing internetconnect object. This is only useful for FTP operations, because FTP is the only protocol for performing multiple operations in the same session. The wininet API caches a separate link handle for each hinternet handle generated by internetopen. Internetopenurl uses this flag for HTTP and FTP connections. Internet_flag_hyperlink: if the server does not return expires time and lastmodified when it determines when to reload from the network, force the reload. Internet_flag_ignore_cert_cn_invalid disable the SSL/PCT-based certificate returned by the slave server for a required host name. The wininet function checks certificates by comparing rules that match host names and wildcards. Internet_flag_ignore_cert_date_invalid the appropriate validity date of the SSL/PCT-based certificate for the disabled check. Internet_flag_ignore_redirect_to_http disable this special redirection. When this flag is used, wininet transparently allows redirection from HTTPS to http url. Internet_flag_ignore_redirect_to_https disable this special redirection. When this flag is used, wininet transparently allows HTTP to https url redirection. Internet_flag_keep_connection if possible, use the retention semantics for the connection. This flag requires Microsoft Network (MSN), NTLM and other types of identity authentication. Internet_flag_need_file: if the file to be created cannot be cached, create a temporary file. Internet_flag_no_auth does not attempt to authenticate automatically. Internet_flag_no_auto_redirect does not automatically process redirection in httpsendrequest. Internet_flag_no_cache_write does not add the returned entity to the cache. Internet_flag_no_cookies do not automatically add the cookie header to the request, and do not automatically add the returned cookie to the cookie database. Internet_flag_no_ui: Disable cookie dialog box. Internet_flag_passive uses Passive FTP semantics. Internetopenurl uses this flag for FTP files and directories. Internet_flag_pragma_nocache is required to be returned by the source server even if a cached copy exists in the proxy. When internet_flag_raw_data searches for the gopher directory information, the returned data is used as the gopher_find_data structure. If the retrieved FTP directory information is used as a win32_find_data structure. If this flag is not specified, or you are requested to create a directory of the html version returned by internetopenurl through the CERN proxy. Internet_flag_reload forcibly downloads required files, objects, or directory lists from the original server, rather than from the cache. Internet_flag_resynchronize: the HTTP resource that is reloaded. If the resource has been modified after the last download. All FTP and Gopher resources will be reloaded. Internet_flag_secure uses Secure Transmission semantics. This transmission uses Secure Sockets Layer/dedicated communication technology (SSL/PCT), which makes sense only in HTTP requests.} 0); // return value // if the connection to FTP, Gopher, or http url has been successfully established, a valid handle is returned. If the connection fails, null is returned. To retrieve a specific error message, getlasterror. To determine why access to the server is denied, call internetgetlastresponseinfo. If hopenurl = nil then exit; // FS: = tfile. open (filename, tfilemode. fmopenorcreate, tfileaccess. faread, tfileshare. fsread); FS: = tfilestream. create (filename, fmcreate); try FS. seek (0, sobeginning); dwread: = 1; while (dwread> 0) Do begin internetreadfile (hopenurl, @ temp, maxblocksize, dwread); FS. write (temp, dwread); end; finally FS. free; internetclosehandle (hopenurl); internetclosehandle (hsession); end.

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.