gpt4 book ai didi

autohotkey - WinHttpRequest 超时

转载 作者:行者123 更新时间:2023-12-01 10:37:51 25 4
gpt4 key购买 nike

我正在使用 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/ 自行查看这些测试。并尝试在已关闭的站点上使用上面的代码。你会看到它返回:

The server name or address could not be resolved


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

The URL is invalid

关于autohotkey - WinHttpRequest 超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32616959/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com