By using this site you agree to the use of cookies by Brugbart and our partners.

Learn more

Setting Request Headers With AutoIt

How to set the request headers when performing HTTP requests.

Edited: 2016-01-03 01:29

Last: Making HTTP POST Requests Next: Controlling HTTP Timeouts

AutoIt Logo

This AutoIt Tutorial will focus on how to set the request headers in AutoIt, using the Winhttprequest.5.1 object.

Many websites will perform user-agent sniffing, some do this to prevent certain bots from using the website. It can therefor be necessary to set the HTTP headers, so that the program will appear like its a real browser.

Setting the HTTP Request Headers With AutoIt

Lets start by showing how you can control the user-agent string, please note that we wont show all the code from the earlier examples again – you should place this somewhere after creating your object, and before sending the request.

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

When you set the request header, you are using the SetRequestHeader Method, this can also be used to set other request headers. Another example is shown below, this time also showing where you should use the method.

  $oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
  $oHTTP.Open("GET", "http://brugbart.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://brugbart.dk/")

  $oHTTP.Send()

The above example should even send a request if you attempted to run the code.

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://brugbart.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://brugbart.dk/")

$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 them back to the server when performing new requests. To deliver the cookies, we will create a function to check the directory for 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://brugbart.com/", False)

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

  $oHTTP.Send()