By using this site you agree to the use of cookies.

Learn more

Share via:

Setting Request Headers With AutoIt

How to set the request headers when performing HTTP requests.

Edited: 2017-02-25 13:17

AutoIt Logo

In this tutorial you will learn how to set Request Headers and handle cookies using the Winhttprequest.5.1 object with AutoIt.

The SetRequestHeader Method of the Winhttprequest.5.1 object allows you to set different headers to be sent along with your request.

Setting the user-agent string

Setting the user-agent string can be done using the SetRequestHeader method as shown below.

$oHTTP.SetRequestHeader("User-Agent", "User-Agent=Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0") Gecko/20100101 Firefox/12.0")

When you are interacting with the request headers, you are using the SetRequestHeader Method.

You may want to mimic a popular browser, to avoid getting blocked on certain sites. Some of the most popular browsers are listed in the below table.

Chrome 41.0.2228.0 Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36
Firefox 40.1 Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1
Opera 12.16 Opera/9.80 (X11; Linux i686; Ubuntu/14.10) Presto/2.12.388 Version/12.16
Internet Explorer 11.0 Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; AS; rv:11.0) like Gecko
Edge 12.246 Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.246

The script below shows how the SetRequestHeader Method can be used.

  $oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
  $oHTTP.Open("GET", "http://beamtic.com/", False)

  $oHTTP.SetRequestHeader("User-Agent", "User-Agent=Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0")
  $oHTTP.SetRequestHeader("Referer", "http://beamtic.com/")

  $oHTTP.Send()

Handling Cookies with AutoIt

Cookies are also delivered with the SetRequestHeader Method, this is useful to know if you are making a program that should be able to login on a website.

But before you can deliver cookies, you will need to have some cookies to be delivered. When making HTTP Requests, any cookies sent by the server, will be available trough the GetAllResponseHeaders method, after the request has been sent.

$oHTTP.Send()
$HeaderResponses = $oHTTP.GetAllResponseHeaders()

The response headers contains more than just the Cookies, so you will need to figure out a way to pick just the cookies. One way to do that is demonstrated below:

$oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
$oHTTP.Open("GET", "http://beamtic.com/", False)

 $oHTTP.SetRequestHeader("User-Agent", "User-Agent=Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0")
 $oHTTP.SetRequestHeader("Referer", "http://beamtic.com/")

$oHTTP.Send()
$HeaderResponses = $oHTTP.GetAllResponseHeaders()


 ; Handle Cookies
$array = StringRegExp($HeaderResponses, 'Set-Cookie: (.+)\r\n', 3)
$cookies = ''
for $i = 0 to UBound($array) - 1
  ; Add all cookies to a single string, and then clean it up.
  $cookies = $array[$i] & ';'
    
  ; Getting the name of the Current Cookie
  $cookname = StringRegExpReplace($cookies, "([^=]+)=.+", "$1")

  ; Removing parts we do not use..
  $cookies = StringRegExpReplace($cookies, "( path| domain| expires)=[^;]+", "")
  $cookies = StringRegExpReplace($cookies, " HttpOnly", "")
  $cookies = StringRegExpReplace($cookies, "[;]{2,}", ";")

; Save the cookies to .txt files
 ; Delete the file if it already exists
 FileDelete ("Cookies\" & $cookname & ".txt")
 $file = FileOpen("Cookies\" & $cookname & ".txt", 1)
 If $file = -1 Then
   MsgBox(0, "Error", "Unable to open file: " & $cookname)
   Exit
 EndIf
 FileWrite($file, $cookies)
 FileClose($file)
	
Next

That was an easy way to handle cookies.

Now we should be able to deliver the cookies back to the server when performing new requests. To deliver the cookies, we will create a function to check the directory to see if there is any cookies to be delivered.

Func DELIVER_COOKIES()
 ; Search for files in the cookie directory and add them to the cookies variable, seperated by ",,,"
 $search = FileFindFirstFile("Cookies\" & "*.*")  
  If $search = -1 Then
     return 'none'
  EndIf

  $cookies = ''
  While 1
    $file = FileFindNextFile($search)
    If @error Then ExitLoop

    $result = StringInStr($file)
  
  If $result > 1 Then
   $cookies = $cookies & ',,,' & $file
  EndIf
	
	
  WEnd
  FileClose($search)

  $cookies = StringTrimLeft($cookies, 3) ; Fix the string

  ; Split the cookies into an array
  ; then read the files one by one, and add their contents to the cookie variable
  $array = StringSplit($cookies, ',,,', 1)

  $counter = 1
  $cookie = ''
  While $counter <= $array[0]
    ;MsgBox(0, "File: ", $array[$counter])
	
    $file = FileOpen("Cookies\" & $array[$counter], 0)
    If $file = -1 Then
      Exit
    EndIf
    $cookie = $cookie & FileRead("Cookies\" & $array[$counter], -1)
    FileClose($file)
    $counter = $counter + 1
  WEnd
  Return $cookie
EndFunc

The above function returns the contents of the cookies as a string, which can be delivered trough the SetRequestHeader Method. When sending the cookies back to the server, simply do something like below:

$Cookie = DELIVER_COOKIES()

  $oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
  $oHTTP.Open("GET", "http://beamtic.com/", False)

   ; Deliver the cookies returned by DELIVER_COOKIES()
   $oHTTP.SetRequestHeader("Cookie", $Cookie)

  $oHTTP.Send()