处理来自 WinHttpRequest 的事件
我使用的程序运行 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我试图在 winhttp 响应到来时执行一些代码(在 HTA 文件中使用 VBScript)。您可以考虑将事件代码放在发送之后。使用以下代码,用户界面在等待响应时不会挂起:
这似乎与脚本文件的同步 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:
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.
将
Open
方法调用中的第三个参数更改为false
。然后将代码放置在调用发送之后的OnResponseFinished
中。Change the third paramater in the call to the
Open
method tofalse
. Then place the code you would have inOnResponseFinished
after the call to send.使用 WScript 的 CreateObject,而不是内置的事件处理程序。
Use WScript's CreateObject not the built in one for event handler.
我承认他的答案不是一个很好的答案,但是注册 VBScript 事件的常用方法是使用 GetRef 函数来获取对事件处理程序的引用,例如使用 MSXML2。 XMLHTTP 对象:
问题是,我尝试了你的代码,即
它不起作用,出现错误
但这也许可以为您提供一个起点,或者您可以改用
MSXML2
库?只需使用处理 COM 事件的其他方式更新此答案 - 使用 CreateObject 函数的第二个参数,该函数允许您指定将函数连接到对象的函数前缀,例如
不幸的是,这也不起作用 - 一定是 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 anMSXML2.XMLHTTP
object:The trouble is, I tried it for your code, ie
and it didn't work, getting the error
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, egunfortunately, this doesn't work either - it must be that the
IWinHttpRequestEvents
interface is inaccessible我检查了 Windows 注册表,似乎有许多 Microsoft 对象执行几乎相同的操作:
对我有用的是 Microsoft.ServerXMLHTTP,它允许在 VBScript 中设置 onreadystatechange。 “MSXML2.ServerXMLHTTP”处理重定向网站(例如google.com),这使其成为比“Microsoft.XMLHTTP”更好的选择。
I checked the Windows Registry and there appears to be a number of Microsoft objects that do nearly the same thing:
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".
根据this(参见备注)您只能通过
Err
访问错误状态。微软的文档很糟糕。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.我发现我可以通过使用带有参数“0”的“waitForResponse”作为超时方法的标志来异步工作。
IE:
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: