Uses Wininet (1) HTTP Download Function Wwwdownfile (const fturl, localfile: string): boolean; // Download function.const ftaccepttypes = '* / *'; ftagent = 'Explorer'; ftusername = '; // username. Ftpassword = ''; // password. Ftport = 80; // port. Ftpostquery = 'get'; ftrefer = ';
Var hSession, hConnect, hRequest: hInternet; HostName, FileName: String; f: File; Buf: Pointer; dwBufLen, dwIndex: DWord; Data: Array [0 .. $ 400] of Char; RequestMethod: PChar; InternetFlag: DWord; TimeOut : Cardinal; AcceptType: LPStr; BytesToRead, BytesReaded: DWord; FTFileSize: integer; Procedure parseURL (URL: String; var HostName, FileName: String); var i: Integer; Begin if Pos ( 'http: //', LowerCase ( URL)) <> 0 THEN System.delete (URL, 1, 7);
I: = POS ('/', url); Hostname: = COPY (URL, 1, I); FileName: = Copy (URL, I, Length (URL) - i 1); if (Length (Host (Hostname)> 0) And (Hostname "] = '/') THEN SETLENGTH (Hostname, Length (Hostname) - 1);
Procedure closehandles; begin internetcloseHandle (HREQUEST); InternetCloseHandle (HConnect); InternetCloseHandle (HSession);
Begin Timeout: = 6000000; Parseurl (fturl, hostname, filename); requestMethod: = pchar (ftpostQuery); // 'get' Internetflag: = 0; AcceptType: = PCHAR ('Accept:' ftaccepttypes);
If FTAGENT <> '' THEN HSESSION: = Internet_Open_Type_Preconfig, NIL, NIL, 0) Else Hsession: = Internetopen (nil, internet_open_type_preconfig, nil, nil, 0);
hConnect: = InternetConnect (hSession, PChar (HostName), FTPort, PChar (FTUserName), PChar (FTPassword), INTERNET_SERVICE_HTTP, 0, 0); hRequest: = HttpOpenRequest (hConnect, RequestMethod, PChar (FileName), 'HTTP / 1.1' , PChar (FTReferer), @AcceptType, InternetFlag, 0); InternetSetOption (hRequest, INTERNET_OPTION_CONNECT_TIMEOUT, @TimeOut, SizeOf (TimeOut)); InternetSetOption (hRequest, INTERNET_OPTION_DATA_RECEIVE_TIMEOUT, @TimeOut, SizeOf (TimeOut)); if FTPostQuery = '' then HttpsendRequest (HREQUEST, NIL, 0, NIL, 0) Else HttpsendRequest (HREQUEST, 'Content-Type: Application / X-WWW-FORM-URLENCODED', 47, PCHAR (FTPOSTQUERY), LENGTH (FTPOSTQUERY));
DWINDEX: = 0; dwbufflen: = 1024; getMem (buf, dwbufflen);
Result: = httpqueryinfo (hRequest, http_query_content_length, buf, dwbufflen, dwindex);
if Result then begin BytesReaded: = 0; FTFileSize: = StrToInt (StrPas (Buf)); AssignFile (f, LocalFile); Rewrite (f, 1); while True do begin if not InternetReadFile (hRequest, @Data, SizeOf (Data ), BytesToRead) then break else if BytesToRead = 0 then Break else BlockWrite (f, Data, BytesToRead); BytesReaded: = BytesReaded BytesToRead; End; Result: = FTFileSize = Integer (BytesReaded); CloseFile (f); End; FreeMem (BUF); CloseHandles; End; (2) FTP Download Function TFORM1.GETLIST (IP: String): Bool; // Get file name list of file names in remote gripping machine VAR HNET, HFTP, HFind: Hinternet; Bsuccess: Boolean; Finddata : TWin32FindData; str: Tstrings; begin //timer1.Enabled:=true; // control: = true; hNet: = InternetOpen ( 'Program_Name', // Agent INTERNET_OPEN_TYPE_PRECONFIG, // AccessType nil, // ProxyName nil, // ProxyBypass 0); // or internet_flag_async / Internet_f LAG_OFFLINE IF HNET = NIL THEN BEGIN MEMO1.LINES.ADD ('Lack of Dynamic Library WinInet.dll!'); Result: = FALSE; EXIT;
Timer1.enabled: = true; Application.ProcessMess (); {Connect to the ftp server} hftp: = Internetconnect (hft, // handle from internetopen pchar ('192.168.0.253'), // ftp server 21, // ( INTERNET_DEFAULT_FTP_PORT), PChar ( 'root'), // username PChar ( ''), // password INTERNET_SERVICE_FTP, // FTP, HTTP, or Gopher 0, // flag:? 0 or INTERNET_FLAG_PASSIVE 255); // User defined number For callback if hftp = nil dam internetclosehandle (hnet); Memo1.Lines.Add ('connection' ip 'success! Ready to start downloading!'); result: = false; exit; end; timer1.enabled: = false; {Change directory} //timer1.Enabled:=true; bSuccess: = FtpSetCurrentDirectory (hFTP, PChar ( '/ mnt / imagefolder')); if not bSuccess then begin InternetCloseHandle (hFTP); InternetCloseHandle (hNet); memo1.Lines .Add ('operating capture machine error, please check! '); Result: = false; exit; end; timer1.enabled: = false;
Hfind: = ftpfindfirstfile (HFTP, '* .jpg', Finddata, 0, 0); if Hfind = nil the beginning (no pictures in the capture machine! '); InternetCloseHandle (HFTP); InternetCloseHandle HNET: = FALSE; EXIT;
Str: = tstringlist.create; str.clear; while internetFindNextFile (hfind, @finddata) do begin str.add (finddata.cfilename);
InternetCloseHandle (HFIND); InternetCloseHandle (HFTP); InternetCloseHandle (HNET); Result: = true;
END;
function TForm1.download (ReFile, LoFile, ip: string): Bool; var Hnet, Hftp: HINTERNET; bSuccess: Boolean; bGetFile: Boolean; begin hNet: = InternetOpen ( 'Program_Name', // Agent INTERNET_OPEN_TYPE_PRECONFIG, // AccessType nil , // proxyName nil, // proxybypass 0); // or internet_flag_async / Internet_flag_offline if hnet = nil the beginning ('lacking dynamic connection library wininet.dll!'); Result: = false; EXIT; END ;
{Connect to the ftp server} hftp: = Internetconnect (hNet, // Handle from Internetopen Pchar (IP), // FTP Server 21, // (Internet_Default_ftp_port), Pchar ('root'), // username pchar ('' ), // password INTERNET_SERVICE_FTP, // FTP, HTTP, or Gopher INTERNET_FLAG_PASSIVE, // flag:? 0 or INTERNET_FLAG_PASSIVE 0); // User defined number for callback if hFTP = nil then begin InternetCloseHandle (hNet); memo1.Lines. Add (IP 'does not exist!'); result: = false; exit;
{Change directory} bSuccess: = FtpSetCurrentDirectory (hFTP, PChar ( '/ mnt / imagefolder')); if not bSuccess then begin InternetCloseHandle (hFTP); InternetCloseHandle (hNet); memo1.Lines.Add ( 'operating errors, check! '); Result: = false; exit;
memo1.Lines.Add ( 'downloading!' ReFile); bGetFile: = ftpgetfile (hftp, PChar (ReFile), pchar (LoFile), false, File_Attribute_Normal, Ftp_Transfer_Type_Binary, 0); if not bGetFile then begin memo1.Lines. Add (Refile 'Download Failed!'); Inc (Errnum); InternetCloseHandle (HFTP); InternetCloseHandle (HNET); Result: = false; exit; end; memo1.lines.add (Refile 'download success!'); SLEEP 20); InternetCloseHandle (HFTP); InternetCloseHandle (HNET);