Async HTTP Requests
I couldn't find the thread for "Async HTTP requests" and the corresponding code snippet, so I rewrote it.
This code snippet sents Async GET requests to an array of URLs, and returns response code, response headers and response body as an array.
#include-once
#include <Array.au3>
Local $HTTP_METHOD = "GET"
Local $ASYNC_REQUEST = True
Local $aURLs = ["https://en.wikipedia.org", "https://en.wikipedia.org/wiki/Forest_raven", "https://en.wikipedia.org/wiki/Stanley_Bruce", "https://en.wikipedia.org/wiki/Beatlemania"]
Local $nTotalThreads = UBound($aURLs)
Local $aRequestHandles[$nTotalThreads]
Local $aThreadStatus[$nTotalThreads]
Local $nCompletedThreads = 0
_ArrayColInsert($aURLs, 1) ; stores response code
_ArrayColInsert($aURLs, 1) ; stores response headers
_ArrayColInsert($aURLs, 1) ; stores response text
For $i = 0 To $nTotalThreads - 1
$aRequestHandles[$i] = ObjCreate("WinHttp.WinHttpRequest.5.1")
$aRequestHandles[$i].Open($HTTP_METHOD, $aURLs[$i][0], $ASYNC_REQUEST)
$aRequestHandles[$i].Send()
Next
Do
For $i = 0 To $nTotalThreads - 1
If $aThreadStatus[$i] = 0 And $aRequestHandles[$i].WaitForResponse(0) = True Then
$aURLs[$i][1] = $aRequestHandles[$i].Status
$aURLs[$i][2] = $aRequestHandles[$i].GetAllResponseHeaders
$aURLs[$i][3] = $aRequestHandles[$i].ResponseText
$aThreadStatus[$i] = 1
$nCompletedThreads += 1
EndIf
Next
Until $nCompletedThreads = $nTotalThreads
_ArrayDisplay($aURLs)
Explanation:
First, we define variables:
$HTTP_METHOD is set to "GET" as the request method.
$ASYNC_REQUEST is set to True for asynchronous processing.
$aURLs is an array of 4 URLs to be requested.
$nTotalThreads is set to the upper bound of $aURLs, which is the number of elements in the array.
$aRequestHandles is an array to store the request handles, with a length of $nTotalThreads.
$aThreadStatus is an array to store the status of each thread, with a length of $nTotalThreads.
$nCompletedThreads is a counter for the completed threads, which is initialized to 0.
The next 3 lines insert 3 new columns in $aURLs to store the response status code, headers, and text.
The next block of code performs a loop from 0 to $nTotalThreads - 1. Inside the loop, an object is created from the "WinHttp.WinHttpRequest.5.1" class and assigned to $aRequestHandles[$i]. The object's Open method is then called, with arguments $HTTP_METHOD, $aURLs[$i][0], and $ASYNC_REQUEST, to initiate the request. The object's Send method is then called to send the request.
The next block of code is a do-until loop that waits until all threads have completed.
Inside the loop, a nested for loop is performed from 0 to $nTotalThreads - 1. If a thread is not yet completed (indicated by $aThreadStatus[$i] being equal to 0) and has received a response (indicated by $aRequestHandles[$i].WaitForResponse(0) being equal to True), then the response status code, headers, and text are stored in $aURLs[$i][1], $aURLs[$i][2], and $aURLs[$i][3] respectively.
The thread status is then set to completed (indicated by $aThreadStatus[$i] being equal to 1) and the $nCompletedThreads counter is incremented.
The last line calls the _ArrayDisplay function with $aURLs as an argument, which displays the contents of the array.