#include <Date.au3>
$sURL = "http://cdburnerxp.se/downloadsetup.exe"
$sFileDate = -1
$aSplit_URL = _HTTPURLSplit($sURL)
$sResponse = _HTTPGetResponse($aSplit_URL[0], $aSplit_URL[1])
If StringInStr($sResponse, 'Location:') Then
$sURL = StringRegExpReplace($sResponse, "(?s).*Location: (.*?)[\r\n].*", "\1")
$aSplit_URL = _HTTPURLSplit($sURL)
$sResponse = _HTTPGetResponse($aSplit_URL[0], $aSplit_URL[1])
$sFileDate = _HTTPGetDate(StringRegExpReplace($sResponse, "(?s).*Last-Modified: (.*?)[\r\n].*", "\1"), "%.4i%.2i%.2i%.2i%.2i%.2i")
EndIf
$sFileName = StringRegExpReplace($sURL, '^.*/', '')
InetGet($sURL, @ScriptDir & '\' & $sFileName)
FileSetTime(@ScriptDir & '\' & $sFileName, $sFileDate)
Func _HTTPGetResponse($sHost, $sPage)
TCPStartup()
Local $sName_To_IP = TCPNameToIP($sHost)
Local $iSocket = TCPConnect($sName_To_IP, 80)
If $iSocket = -1 Then
TCPShutdown()
Return SetError(1, 0, "")
EndIf
Local $sCommand = "HEAD " & $sPage & " HTTP/1.1" & @CRLF
$sCommand &= "Host: " & $sHost & @CRLF
$sCommand &= "User-Agent: AutoIt/" & @AutoItVersion & " (Windows; U; Windows NT 5.1; en-US; rv:1.8.1)" & @CRLF
$sCommand &= "Referer: " & $sHost & @CRLF
$sCommand &= "Connection: close" & @CRLF & @CRLF
Local $BytesSent = TCPSend($iSocket, $sCommand)
If $BytesSent = 0 Then
TCPShutdown()
Return SetError(2, @error, 0)
EndIf
Local $sRecv = "", $sCurrentRecv
While 1
$sCurrentRecv = TCPRecv($iSocket, 16)
If @error <> 0 Then ExitLoop
If $sCurrentRecv <> "" Then $sRecv &= $sCurrentRecv
WEnd
TCPCloseSocket($iSocket)
TCPShutdown()
Return $sRecv
EndFunc
Func _HTTPURLSplit($sURL)
Local $aRet[2] = [StringRegExpReplace($sURL, ".*://(.*?)/.*", "\1"), StringRegExpReplace($sURL, ".*://.*?(/.*)", "\1")]
Return $aRet
EndFunc
Func _HTTPGetDate($sDate, $sFormat = "%.4i/%.2i/%.2i %.2i:%.2i:%.2i")
$aSplit = StringRegExp($sDate, "(?i)\w+, (\d+) (\w+) (\d{4}) (\d+):(\d+):(\d)", 3)
$iDay = $aSplit[0]
$iMonth = $aSplit[1]
$iYear = $aSplit[2]
$iHour = $aSplit[3]
$iMin = $aSplit[4]
$iSec = $aSplit[5]
For $i = 1 To 12
If _DateToMonth($i, 1) = $aSplit[1] Then
$iMonth = $i
ExitLoop
EndIf
Next
Return StringFormat($sFormat, $iYear, $iMonth, $iDay, $iHour, $iMin, $iSec)
EndFunc