Advertisement
Guest User

Untitled

a guest
Jan 7th, 2012
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 2.19 KB | None | 0 0
  1. #RequireAdmin ; Just for manipulating hosts file
  2. #include "WinHttp.au3"
  3. main()
  4.  
  5. Func main()
  6.  
  7.     Local Const $Hostname = "www.autoitscript.com"
  8.     Local Const $URL = "http://" & $Hostname & "/"
  9.     Local Const $HostnameToLocalhost = "127.0.0.1 " & $Hostname ; redirect the hostname to localhost IP
  10.     Local Const $hosts = @WindowsDir & "\system32\drivers\etc\hosts"
  11.  
  12.     $STRSource = GetSource($URL)
  13.  
  14.     MsgBox(0,"Before redirection",$STRSource) ; displays data before redirection
  15.  
  16.     ;backup hosts
  17.  
  18.     $hostsHandle = FileOpen($hosts)
  19.     $hostsBackup = FileRead($hostsHandle)
  20.     FileClose($hostsHandle)
  21.  
  22.     ;redirect hostname to localhost
  23.  
  24.     $hostsHandle = FileOpen($hosts, 1)
  25.     FileWrite($hostsHandle, @CRLF & $HostnameToLocalhost)
  26.     FileClose($hostsHandle)
  27.  
  28.     ;delay for redirection
  29.  
  30.     Sleep(5000)
  31.  
  32.     ;try to get source code again
  33.  
  34.     $STRSource = GetSource($URL)
  35.  
  36.     MsgBox(0,"After redirection", $STRSource) ; displays data after redirection
  37.  
  38.     ;restore hosts
  39.  
  40.     $hostsHandle = FileOpen($hosts, 2)
  41.     FileWrite($hostsHandle, $hostsBackup)
  42.     FileClose($hostsHandle)
  43.  
  44. EndFunc
  45.  
  46. Func GetSource(Const $URL)
  47.  
  48.     Local $sData = ""
  49.     Local $hOpen = _WinHttpOpen()
  50.     Local $crackedURL = _WinHttpCrackUrl($URL) ;crackURL
  51.     Local $hostname = $crackedURL[2]
  52.     Local $URLPath = $crackedURL[6]
  53.  
  54.     $hConnect = _WinHttpConnect($hOpen, $hostname)
  55.  
  56.     $hRequest = _WinHttpOpenRequest($hConnect, "GET", $URLPath); Request page's source code
  57.     _WinHttpSendRequest($hRequest)
  58.     If @error Then
  59.         CloseHandle($hRequest, $hConnect, $hOpen)
  60.         Return -1
  61.     EndIf
  62.  
  63.     _WinHttpReceiveResponse($hRequest)
  64.     If @error Then
  65.         CloseHandle($hRequest, $hConnect, $hOpen)
  66.         Return -1
  67.     EndIf
  68.  
  69.     If _WinHttpQueryDataAvailable($hRequest) Then
  70.         ; Read
  71.         While 1
  72.             $sChunk = _WinHttpReadData($hRequest)
  73.             If @error Then ExitLoop
  74.             $sData &= $sChunk
  75.         WEnd
  76.     Else
  77.         CloseHandle($hRequest, $hConnect, $hOpen)
  78.         Return -1
  79.     EndIf
  80.  
  81.     ; Close handles when they are not needed any more
  82.     CloseHandle($hRequest, $hConnect, $hOpen)
  83.  
  84.     Return $sData
  85.  
  86. EndFunc   ;==>GetSource
  87.  
  88. Func CloseHandle(Const $Request, Const $Connect, Const $Open)
  89.  
  90.     _WinHttpCloseHandle($Request)
  91.     _WinHttpCloseHandle($Connect)
  92.     _WinHttpCloseHandle($Open)
  93.  
  94. EndFunc   ;==>CloseHandle
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement