处理来自 WinHttpRequest 的事件

发布于 2024-10-19 20:12:21 字数 286 浏览 7 评论 0原文

我使用的程序运行 .VBS 脚本

那么,在 VBScript 中,如何处理 WinHttpRequest 对象的 OnResponseFinished 事件呢?

Set oHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
oHTTP.Open "GET", "http://www.google.com", True
oHTTP.Send

A program I use runs .VBS scripts

So, in VBScript how can you handle the OnResponseFinished event for a WinHttpRequest object?

Set oHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
oHTTP.Open "GET", "http://www.google.com", True
oHTTP.Send

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

岛歌少女 2024-10-26 20:12:21

我试图在 winhttp 响应到来时执行一些代码(在 HTA 文件中使用 VBScript)。您可以考虑将事件代码放在发送之后。使用以下代码,用户界面在等待响应时不会挂起:

Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
objHTTP.Open "GET", "http://www.google.com", True
objHTTP.Send
objHTTP.WaitForResponse 'pauses execution, but does not hang UI
'from now on, execution only takes effect after completion of the response:
msgbox objHTTP.responseText 'an example of what can be done with the response

这似乎与脚本文件的同步 winhttp 相同,这可能就是您正在寻找的。因此,在使用用户界面时可能会注意到唯一的区别。

I was trying to get some code executed when the winhttp response comes (using VBScript inside HTA file). You may consider putting your event code right after the send. Using the following code, the user interface does not hang when waiting for the response:

Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
objHTTP.Open "GET", "http://www.google.com", True
objHTTP.Send
objHTTP.WaitForResponse 'pauses execution, but does not hang UI
'from now on, execution only takes effect after completion of the response:
msgbox objHTTP.responseText 'an example of what can be done with the response

This seems to be the same as synchronous winhttp for script files, which can be what you are looking for. So, the only difference may be noticed when using an user interface.

合久必婚 2024-10-26 20:12:21

Open 方法调用中的第三个参数更改为 false。然后将代码放置在调用发送之后的 OnResponseFinished 中。

Change the third paramater in the call to the Open method to false. Then place the code you would have in OnResponseFinished after the call to send.

夏末染殇 2024-10-26 20:12:21

使用 WScript 的 CreateObject,而不是内置的事件处理程序。

Set oHTTP = WScript.CreateObject(
    "WinHttp.WinHttpRequest.5.1",
    "oHTTP_"
)

Use WScript's CreateObject not the built in one for event handler.

Set oHTTP = WScript.CreateObject(
    "WinHttp.WinHttpRequest.5.1",
    "oHTTP_"
)
┾廆蒐ゝ 2024-10-26 20:12:21

我承认他的答案不是一个很好的答案,但是注册 VBScript 事件的常用方法是使用 GetRef 函数来获取对事件处理程序的引用,例如使用 MSXML2。 XMLHTTP 对象:

Set oHTTP = CreateObject("MSXML2.XMLHTTP")
oHTTP.Open "GET", "http://www.google.com", True

oHTTP.OnReadyStateChange = GetRef("oHTTP_OnReadyStateChange")

Sub oHTTP_OnReadyStateChange
    ' do something
End sub

oHTTP.Send

问题是,我尝试了你的代码,即

Set oHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
oHTTP.Open "GET", "http://www.google.com", True

oHTTP.OnResponseFinished = GetRef("oHTTP_OnResponseFinished")

Sub oHTTP_OnResponseFinished
    ' do something
End sub

oHTTP.Send

它不起作用,出现错误

对象不支持此属性或方法:“oHTTP.OnResponseFinished”

但这也许可以为您提供一个起点,或者您可以改用 MSXML2 库?

只需使用处理 COM 事件的其他方式更新此答案 - 使用 CreateObject 函数的第二个参数,该函数允许您指定将函数连接到对象的函数前缀,例如

Set oHTTP = CreateObject("WinHttp.WinHttpRequest.5.1", "oHTTP_")
oHTTP.Open "GET", "http://www.google.com", True

Sub oHTTP_OnResponseFinished
    ' do something
End sub

oHTTP.Send

不幸的是,这也不起作用 - 一定是 IWinHttpRequestEvents 接口无法访问

I'll admit his isn't a great answer, but the usual way to register VBScript events is to use the GetRef function to get a reference to the event handler, eg with an MSXML2.XMLHTTP object:

Set oHTTP = CreateObject("MSXML2.XMLHTTP")
oHTTP.Open "GET", "http://www.google.com", True

oHTTP.OnReadyStateChange = GetRef("oHTTP_OnReadyStateChange")

Sub oHTTP_OnReadyStateChange
    ' do something
End sub

oHTTP.Send

The trouble is, I tried it for your code, ie

Set oHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
oHTTP.Open "GET", "http://www.google.com", True

oHTTP.OnResponseFinished = GetRef("oHTTP_OnResponseFinished")

Sub oHTTP_OnResponseFinished
    ' do something
End sub

oHTTP.Send

and it didn't work, getting the error

Object doesn't support this property or method: 'oHTTP.OnResponseFinished'

but perhaps this can give you a starting point , or maybe you can use the MSXML2 library instead?

Just updating this answer with the other way of handling COM events - use the second parameter for the CreateObject function which allows you to specify the function prefix which connects functions to objects, eg

Set oHTTP = CreateObject("WinHttp.WinHttpRequest.5.1", "oHTTP_")
oHTTP.Open "GET", "http://www.google.com", True

Sub oHTTP_OnResponseFinished
    ' do something
End sub

oHTTP.Send

unfortunately, this doesn't work either - it must be that the IWinHttpRequestEvents interface is inaccessible

狼性发作 2024-10-26 20:12:21

我检查了 Windows 注册表,似乎有许多 Microsoft 对象执行几乎相同的操作:

Microsoft.XMLHTTP {ED8C108E-4349-11D2-91A4-00C04F7969E8}
MSXML2.XMLHTTP {F6D90F16-9C73-11D3-B32E-00C04F990BB4}
WinHttp.WinHttpRequest.5.1 {2087c2f4-2cef-4953-a8ab-66779b670495}
MSXML2.ServerXMLHTTP {AFBA6B42-5692-48EA-8141-DC517DCF0EF1}

对我有用的是 Microsoft.ServerXMLHTTP,它允许在 VBScript 中设置 onreadystatechange。 “MSXML2.ServerXMLHTTP”处理重定向网站(例如google.com),这使其成为比“Microsoft.XMLHTTP”更好的选择。

Dim xmlhttp ' global so can be accessed in OnStateChange

Sub OnStateChange
    If xmlhttp.readystate = 4 Then
        ' React to xmlhttp.responseText
        MsgBox xmlhttp.responseText
    End If
End Sub

Set xmlhttp = CreateObject("Microsoft.XMLHTTP")
xmlhttp.open "GET", "http://www.google.com/", true
xmlhttp.onreadystatechange = GetRef("OnStateChange")
xmlhttp.send
' do something else whilst xmlhttp is running in the background
MsgBox "Pausing so that OnStateChange can fire!"

I checked the Windows Registry and there appears to be a number of Microsoft objects that do nearly the same thing:

Microsoft.XMLHTTP {ED8C108E-4349-11D2-91A4-00C04F7969E8}
MSXML2.XMLHTTP {F6D90F16-9C73-11D3-B32E-00C04F990BB4}
WinHttp.WinHttpRequest.5.1 {2087c2f4-2cef-4953-a8ab-66779b670495}
MSXML2.ServerXMLHTTP {AFBA6B42-5692-48EA-8141-DC517DCF0EF1}

What works for me is Microsoft.ServerXMLHTTP which allows setting of the onreadystatechange in VBScript. The "MSXML2.ServerXMLHTTP" handles redirecting websites (e.g. google.com) which makes it a better choice over "Microsoft.XMLHTTP".

Dim xmlhttp ' global so can be accessed in OnStateChange

Sub OnStateChange
    If xmlhttp.readystate = 4 Then
        ' React to xmlhttp.responseText
        MsgBox xmlhttp.responseText
    End If
End Sub

Set xmlhttp = CreateObject("Microsoft.XMLHTTP")
xmlhttp.open "GET", "http://www.google.com/", true
xmlhttp.onreadystatechange = GetRef("OnStateChange")
xmlhttp.send
' do something else whilst xmlhttp is running in the background
MsgBox "Pausing so that OnStateChange can fire!"
凉世弥音 2024-10-26 20:12:21

It does need seem to be possible according to this (go to remarks) you can only access the error state with Err. Microsoft's documentation is lousy.

み格子的夏天 2024-10-26 20:12:21

我发现我可以通过使用带有参数“0”的“waitForResponse”作为超时方法的标志来异步工作。

IE:

 oHTTP.Open "GET", "http://www.google.com", True
 oHTTP.Send

 Do While oHTTP.waitForResponse(0) = False
   'do stuff while waiting for it to be done

    WScript.Sleep 200 'sleep for 0.2 seconds between checks as not waste CPU 
    DoEvents
 Loop

 'Once the loop is exited, the response is finished
 MsgBox oHTTP.ResponseText

I found that I can get this to work Asynchronously by using the 'waitForResponse' with parameter '0' for the timeout method as a flag.

IE:

 oHTTP.Open "GET", "http://www.google.com", True
 oHTTP.Send

 Do While oHTTP.waitForResponse(0) = False
   'do stuff while waiting for it to be done

    WScript.Sleep 200 'sleep for 0.2 seconds between checks as not waste CPU 
    DoEvents
 Loop

 'Once the loop is exited, the response is finished
 MsgBox oHTTP.ResponseText
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文