Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #RequireAdmin ; Just for manipulating hosts file
- #include "WinHttp.au3"
- main()
- Func main()
- Local Const $Hostname = "www.autoitscript.com"
- Local Const $URL = "http://" & $Hostname & "/"
- Local Const $HostnameToLocalhost = "127.0.0.1 " & $Hostname ; redirect the hostname to localhost IP
- Local Const $hosts = @WindowsDir & "\system32\drivers\etc\hosts"
- $STRSource = GetSource($URL)
- MsgBox(0,"Before redirection",$STRSource) ; displays data before redirection
- ;backup hosts
- $hostsHandle = FileOpen($hosts)
- $hostsBackup = FileRead($hostsHandle)
- FileClose($hostsHandle)
- ;redirect hostname to localhost
- $hostsHandle = FileOpen($hosts, 1)
- FileWrite($hostsHandle, @CRLF & $HostnameToLocalhost)
- FileClose($hostsHandle)
- ;delay for redirection
- Sleep(5000)
- ;try to get source code again
- $STRSource = GetSource($URL)
- MsgBox(0,"After redirection", $STRSource) ; displays data after redirection
- ;restore hosts
- $hostsHandle = FileOpen($hosts, 2)
- FileWrite($hostsHandle, $hostsBackup)
- FileClose($hostsHandle)
- EndFunc
- Func GetSource(Const $URL)
- Local $sData = ""
- Local $hOpen = _WinHttpOpen()
- Local $crackedURL = _WinHttpCrackUrl($URL) ;crackURL
- Local $hostname = $crackedURL[2]
- Local $URLPath = $crackedURL[6]
- $hConnect = _WinHttpConnect($hOpen, $hostname)
- $hRequest = _WinHttpOpenRequest($hConnect, "GET", $URLPath); Request page's source code
- _WinHttpSendRequest($hRequest)
- If @error Then
- CloseHandle($hRequest, $hConnect, $hOpen)
- Return -1
- EndIf
- _WinHttpReceiveResponse($hRequest)
- If @error Then
- CloseHandle($hRequest, $hConnect, $hOpen)
- Return -1
- EndIf
- If _WinHttpQueryDataAvailable($hRequest) Then
- ; Read
- While 1
- $sChunk = _WinHttpReadData($hRequest)
- If @error Then ExitLoop
- $sData &= $sChunk
- WEnd
- Else
- CloseHandle($hRequest, $hConnect, $hOpen)
- Return -1
- EndIf
- ; Close handles when they are not needed any more
- CloseHandle($hRequest, $hConnect, $hOpen)
- Return $sData
- EndFunc ;==>GetSource
- Func CloseHandle(Const $Request, Const $Connect, Const $Open)
- _WinHttpCloseHandle($Request)
- _WinHttpCloseHandle($Connect)
- _WinHttpCloseHandle($Open)
- EndFunc ;==>CloseHandle
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement