WinHttpRequest 超时

WinHttpRequest timeouts

我正在使用 AHK 脚本发送一些 POST 请求。 我正在尝试获得超时响应,以便我可以向用户弹出一些消息。 我不知道如何使用 "SetTimeouts" 方法和 "WaitForResponse" 请参阅下面的代码

WebRequest := ComObjCreate("WinHttp.WinHttpRequest.5.1")
WebRequest.SetTimeouts(3000,3000,3000,3000)

openConnection(WebRequest,ip){
   WebRequest.Open("POST", "http://" ip "/cgi/drsLogin",true)
   WebRequest.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")
   WebRequest.Send("action=login&username=admin&password=admin")
   time := WebRequest.WaitForResponse(2)

   if (time = -1) {
    addTextToGui("Connection Timeout")
   }
   else{
     return
   }
 return
}

AutoHotkey 喜欢函数中的值被引号包围。

尝试更改:

WebRequest.SetTimeouts(3000,3000,3000,3000)

WinHttpReq.SetTimeouts("30000", "30000", "30000", "30000")

编辑:好的,看来我错了,两种方法都有效。

我使用这个网站测试了以上内容:http://tmplinshi.sinaapp.com/test/timeout-test-20s.php

在下面的代码中,SetTimouts 设置为 10 秒我们的测试设置为在 20 秒内响应,因此这将保证超时。

WinHttpReq := ComObjCreate("WinHttp.WinHttpRequest.5.1")
WinHttpReq.SetTimeouts("10000", "10000", "10000", "10000")
WinHttpReq.Open("GET", "http://tmplinshi.sinaapp.com/test/timeout-test-20s.php", false)
WinHttpReq.Send()
WinHttpReq.WaitForResponse()
webpage := WinHttpReq.ResponseText
MsgBox % webpage

I'm getting a Timeout Error but this breaks the script and still doesn't answer how to pull the Timeout message?

既然我们收到超时错误,这意味着我们需要在代码中添加错误处理。我们将使用 Try / Catch.

来做到这一点
WinHttpReq := ComObjCreate("WinHttp.WinHttpRequest.5.1")
WinHttpReq.SetTimeouts("10000", "10000", "10000", "10000")

Try { 
WinHttpReq.Open("GET", "http://tmplinshi.sinaapp.com/test/timeout-test-20s.php", false)
WinHttpReq.Send()
WinHttpReq.WaitForResponse()
webpage := WinHttpReq.ResponseText
}
Catch e{
    MsgBox % e
    ExitApp
}

MsgBox % webpage

Okay, the script no longer breaks but the MsgBox is blank? This isn't working!

嗯,它是空白的,因为我们的错误消息是作为对象从对象返回的!检查 e 对象,我发现它包含几个键,将数据存储为字符串,甚至是一个整数值。这些键被标记为:Extra、File、Line、Message 和 What...所以让我们来看看 Message!

WinHttpReq := ComObjCreate("WinHttp.WinHttpRequest.5.1")
WinHttpReq.SetTimeouts("10000", "10000", "10000", "10000")

Try { 
WinHttpReq.Open("GET", "http://tmplinshi.sinaapp.com/test/timeout-test-20s.php", false)
WinHttpReq.Send()
WinHttpReq.WaitForResponse()
webpage := WinHttpReq.ResponseText
}
Catch e{
    MsgBox % e.Message
    ExitApp
}

MsgBox % webpage

Okay so now I see:

0x80072EE2 -
Source: WinHttp.WinHttpRequest

Description: The operation timed out

HelpFile: (null)

HelpContext: 0

是的,我们似乎收到了超时消息和一堆我们可能不想要的其他信息。那么我们现在可以做的就是从消息中解析出我们想要的数据!像这样:

WinHttpReq := ComObjCreate("WinHttp.WinHttpRequest.5.1")
WinHttpReq.SetTimeouts("10000", "10000", "10000", "10000")

Try { 
WinHttpReq.Open("GET", "http://tmplinshi.sinaapp.com/test/timeout-test-20s.php", false)
WinHttpReq.Send()
WinHttpReq.WaitForResponse()
webpage := WinHttpReq.ResponseText
}
Catch e{
    For Each, Line in StrSplit(e.Message, "`n", "`r") {
        Results := InStr(Line, "Description:") 
            ? StrReplace(Line, "Description:")
            : ""
        If (Results <> "")
            Break
    }
    MsgBox % Trim(Results)
    ExitApp
}

MsgBox % webpage

编辑:

忘记提及此方法不仅会捕获超时,还会捕获各种其他错误,例如无法访问的地址或无效 URL,并且它会正确显示这些错误。

您可以通过转至 http://www.isitdownrightnow.com/ 并尝试在已关闭的站点上使用上面的代码来亲自测试这些。你会看到它 returns:

The server name or address could not be resolved

还将不属于的字符或空格添加到 URL 代码生成:

The URL is invalid

这是另一种方法。它更具可读性,并允许您设置总超时时间,而不是您通常不关心的不同的较低级别超时时间。

WinHttpReq := ComObjCreate("WinHttp.WinHttpRequest.5.1")

timedOut := False
WinHttpReq.Open("GET", "http://google.com", True)
WinHttpReq.Send()
Sleep, 1     ;timeout: 1 millisecond (will time out)
;Sleep, 4000 ;timeout: 4 seconds (should not time out)

ComObjError(False)
WinHttpReq.Status
If (A_LastError) ;if WinHttpReq.Status was not set (no response received yet)
    timedOut := True
ComObjError(True)

If (timedOut)
    MsgBox, timed out
Else
    MsgBox, didn't time out