In this AutoIt Tutorial we will be using the Winhttprequest.5.1 COM object to make a HTTP GET Request. The Winhttprequest.5.1 COM object is also available in other programming languages, reading tutorials and references for those can also help you understand how it works for AutoIt.
We will attempt to make a GET Request, and catch the HTTP Response code, as well as download the body of the response. There are two parts of a HTTP Request, the head, and the body. The head contains all the headers and response codes, while the body contains the content of the resource (I.e. text/html, Images, and so on).
AutoIt HTTP GET Request
To make the Request, we first need to create a new instance of the Winhttprequest.5.1 object – if you are new to objects, try to think of it as a real object that you can turn, flip, and work with in your hands – in a much similar way, you will be able to do the same with an object in a programming language.
The object is really just a variable that you would name as any other variable.
$oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
The next thing to do, is to give the object the URL that you want to fetch, this can be done with the Open method.
$oHTTP.Open("GET", "http://brugbart.com/", False)
Finally we can send the Request, this is being done with the Send method.
This would give us the following AutoIt Script.
$oHTTP = ObjCreate("winhttp.winhttprequest.5.1") $oHTTP.Open("GET", "http://brugbart.com/", False) $oHTTP.Send()
Working with the Response Body
Let us add a couple of details – lets fetch the HTTP Response Code, and download the content of the requested resource. First, after sending the request using Send, we can use the below to download the content of the resource.
$oReceived = $oHTTP.ResponseText
The above will retrieve the body as text, and store it in the $oReceived variable, to see if it works, you can save it to a file.
See: Write Content to a File with AutoIt to learn how to do that.
Getting the Response Code
Next we should get the Response code of the HTTP Request, we can use this in our script to make decisions depending on the response we get from the server. The status message can be retrieved using the Status property.
$oStatusCode = $UrlCheck.Status
The above saves the status code into the $oStatusCode variable, we can use this in if statements to make decisions depending on the response code. The response code is just a number, such as 404 for the well known 404 Not Found response.
$oStatusCode = $oHTTP.Status If $oStatusCode == 200 then ; Do some stuff here EndIf
The Completed Script
For those of you who have a hard time putting the above together, the below script should work out-of-the-box.
$oHTTP = ObjCreate("winhttp.winhttprequest.5.1") $oHTTP.Open("GET", "http://brugbart.com/", False) $oHTTP.Send() $oReceived = $oHTTP.ResponseText $oStatusCode = $oHTTP.Status If $oStatusCode == 200 then $file = FileOpen("Received.html", 2) ; The value of 2 overwrites the file if it already exists FileWrite($file, $oReceived) FileClose($file) EndIf