Downloads a file from the internet using the HTTP, HTTPS or FTP protocol.
InetGet ( "URL", "filename" [, options = 0 [, background = 0]] )
URL | URL of the file to download. See remarks below. |
filename | Local filename to download to. |
options | [optional]$INET_LOCALCACHE (0) = Get the file from local cache if available (default). $INET_FORCERELOAD (1) = Forces a reload from the remote site. $INET_IGNORESSL (2) = Ignore all SSL errors (with HTTPS connections). $INET_ASCIITRANSFER (4) = Use ASCII when transferring files with the FTP protocol (Can not be combined with flag $INET_BINARYTRANSFER (8)). $INET_BINARYTRANSFER (8) = Use BINARY when transferring files with the FTP protocol (Can not be combined with flag $INET_ASCIITRANSFER (4)). This is the default transfer mode if none are provided. $INET_FORCEBYPASS (16) = By-pass forcing the connection online (See remarks). Constants are defined in InetConstants.au3. |
background | [optional] $INET_DOWNLOADWAIT (0) = Wait until the download is complete before continuing (default). $INET_DOWNLOADBACKGROUND (1) = return immediately and download in the background (see remarks). Constants are defined in InetConstants.au3 |
Success: | the return value changes depending on if the download is in the background: Background: a handle is returned. Wait: the number of bytes downloaded. |
Failure: | Background: a handle is returned. Wait: 0 and sets the flag to non-zero. |
Use the returned handle with InetGetInfo() to determine if there was an error with the download.
The returned handle must be closed with InetClose().
The URL parameter should be in the form "http://www.somesite.com/path/file.html" - just like an address you would type into your web browser.
To use a username and password when connecting simply prefix the servername with "username:password@", e.g.
"http://myuser:mypassword@www.somesite.com"
Notes about the "background" Parameter
By default the function waits until the download has finished before returning. If the background parameter is set to $INET_DOWNLOADBACKGROUND (1) the function returns immediately and the download continues in the background. The function InetGetInfo() can be used to check the status of the download. It takes the handle returned from InetGet().
Multiple downloads are supported if they are started in background mode.
To abort a download call InetClose() and pass it the handle returned by InetGet().
By default AutoIt forces a connection before starting a download. For dial-up users this will prompt to go online or dial the modem (depending on how the system is configured). The options value $INET_FORCEBYPASS (16) disables this behavior. Disabling the behavior can be useful for persistent connects (Broadband, LAN). However, it is also required to work around certain issues in Windows Vista and Windows 7.
FtpSetProxy, HttpSetProxy, HttpSetUserAgent, InetClose, InetGetInfo, InetGetSize, InetRead
#include <InetConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>
; InetGet downloads a file in the background.
; The AutoIt script checks in a loop for the download to complete.
Example()
Func Example()
; Save the downloaded file to the temporary folder.
Local $sFilePath = _WinAPI_GetTempFileName(@TempDir)
; Download the file in the background with the selected option of 'force a reload from the remote site.'
Local $hDownload = InetGet("http://www.autoitscript.com/autoit3/files/beta/update.dat", $sFilePath, $INET_FORCERELOAD, $INET_DOWNLOADBACKGROUND)
; Wait for the download to complete by monitoring when the 2nd index value of InetGetInfo returns True.
Do
Sleep(250)
Until InetGetInfo($hDownload, $INET_DOWNLOADCOMPLETE)
; Retrieve the number of total bytes received and the filesize.
Local $iBytesSize = InetGetInfo($hDownload, $INET_DOWNLOADREAD)
Local $iFileSize = FileGetSize($sFilePath)
; Close the handle returned by InetGet.
InetClose($hDownload)
; Display details about the total number of bytes read and the filesize.
MsgBox($MB_SYSTEMMODAL, "", "The total download size: " & $iBytesSize & @CRLF & _
"The total filesize: " & $iFileSize)
; Delete the file.
FileDelete($sFilePath)
EndFunc ;==>Example
#include <InetConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>
; InetGet waits for the download to complete.
Example()
Func Example()
; Save the downloaded file to the temporary folder.
Local $sFilePath = _WinAPI_GetTempFileName(@TempDir)
; Download the file by waiting for it to complete. The option of 'get the file from the local cache' has been selected.
Local $iBytesSize = InetGet("http://www.autoitscript.com/autoit3/files/beta/update.dat", $sFilePath, $INET_FORCERELOAD)
; Retrieve the filesize.
Local $iFileSize = FileGetSize($sFilePath)
; Display details about the total number of bytes read and the filesize.
MsgBox($MB_SYSTEMMODAL, "", "The total download size: " & $iBytesSize & @CRLF & _
"The total filesize: " & $iFileSize)
; Delete the file.
FileDelete($sFilePath)
EndFunc ;==>Example