I'm trying to perform a web request using AHK that will time out after 1 second. This script will be called after a certain key combination.
For my smartphone I have a small HTML app that does the call using AJAX and times out properly after 1 second.
This is the AHK script:
SendCode(code) {
HttpObj := ComObjCreate("WinHttp.WinHttpRequest.5.1")
try {
HttpObj.Open("GET","http://192.168.1.120/?" + code)
HttpObj.SetTimeouts(1000,1000,1000,1000)
HttpObj.Send()
} catch e {
MsgBox, Error executing code: %code%
}
ObjRelease(HttpObj)
}
SendCode(24)
SendCode(26)
Since I'm more proficient with C# I also tried it with a .NET console application:
static void Main(string[] args)
{
const string url = "http://192.168.1.121/?24";
const int timeout = 1000;
double stopWatch;
dynamic HttpObj = Activator.CreateInstance(Type.GetTypeFromProgID("WinHttp.WinHttpRequest.5.1"));
HttpObj.Open("GET", url);
HttpObj.SetTimeouts(timeout, timeout, timeout, timeout);
stopWatch = DateTime.Now.TimeOfDay.TotalMilliseconds;
try
{
HttpObj.Send();
}
catch
{
}
stopWatch = DateTime.Now.TimeOfDay.TotalMilliseconds - stopWatch;
Console.WriteLine(stopWatch);
Console.Read();
}
This outputs
4029,00729998946
When I don't specify SetTimeouts the timeout occurs after 30 seconds. Is there anyway to do this using WinHttpRequest?
Using the .NET WebClient class I can also set a timeout of 1s without problems.
Thanks