【问题标题】:Asynchronous HttpRequest using WinHttp.WinHttpRequest.5.1 in ASP在 ASP 中使用 WinHttp.WinHttpRequest.5.1 的异步 HttpRequest
【发布时间】:2013-12-31 09:02:21
【问题描述】:

我正在尝试制作 LINK FINDER 并面临 2 个问题

问题 1(已解决) :: 无法获取重定向页面的 url

这已通过使用 WinHttp.WinHttpRequest.5.1

解决 REFERNCE LINK

问题 2(未解决) :: 无法使用 WinHttp.WinHttpRequest.5.1 对象 EVENTS 或者没有回调异步请求

同步请求代码

Set req = CreateObject("WinHttp.WinHttpRequest.5.1")
req.open "GET", url, FALSE
req.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
req.send PostData

这工作正常,但如果我有多个请求,那么它需要很多时间。

我尝试了异步请求代码,但得到错误

Set req = CreateObject("WinHttp.WinHttpRequest.5.1")
req.open "GET", url, TRUE
req.OnReadyStateChange = GetRef("req_OnReadyStateChange")
req.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
req.send PostData

Function req_OnReadyStateChange
   ' do something
End Function  

代码 1

Set req = CreateObject("WinHttp.WinHttpRequest.5.1","req_")
req.open "GET", url, TRUE
Function req__OnResponseFinished
  ' do something
End Function
req.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
req.send PostData

错误 - 远程服务器机器不存在或不可用:'CreateObject'

代码 2

Set req = CreateObject("WinHttp.WinHttpRequest.5.1")
req.open "GET", url, TRUE
req.OnResponseFinished = GetRef("req_OnResponseFinished")
Function req_OnResponseFinished
   ' do something
End Function
req.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
req.send PostData

错误:对象不支持此属性或方法:'req.OnResponseFinished

代码 3

Set req = CreateObject("WinHttp.WinHttpRequest.5.1")
req.open "GET", url, TRUE
req.OnReadyStateChange = GetRef("req_OnReadyStateChange")
req.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
req.send PostData
 Function req_OnReadyStateChange
   ' do something
End Function

在微软文档中,他们提到了 WinHttp.WinHttpRequest.5.1 有 4 个事件。

  1. OnError
  2. OnResponseDataAvailable
  3. OnResponseFinished
  4. OnResponseStart

但是我没有得到如何使用这个事件的例子,我也不能在 ASP 中使用这些事件。

希望快速响应...

【问题讨论】:

    标签: asynchronous asp-classic event-handling httprequest


    【解决方案1】:

    您是否尝试过为“req_OnReadyStateChange”使用 Sub 而不是函数?

    顺便说一句,我正在使用 MSXML2.ServerXMLHTTP 对象,这工作正常。你有什么理由使用这个 WinHttp API?

    MSXML2.ServerXMLHTTP 示例:

    <%
    dim url : url = "http://localhost"
    dim XmlHttp : set XmlHttp = server.createobject("MSXML2.ServerXMLHTTP")
    XmlHttp.onreadystatechange = getRef("doHttpReadyStateChange")
    XmlHttp.open "GET", url, true
    XmlHttp.send()
    
    sub doHttpReadyStateChange
        response.write XmlHttp.readyState
        response.write "<br>"
    
        select case XmlHttp.readyState
            case 0  'UNINITIALIZED
    
            case 1  'LOADING
    
            case 2  'LOADED
    
            case 3  'INTERACTIVE
    
            case 4  'COMPLETED
                response.write "Done"
        end select
    end sub
    %>
    

    【讨论】:

    • 是的,重定向后我需要最终 URL,这就是我使用 WinHttp.WinHttpRequest.5.1 的原因。欲了解更多信息,您可以查看stackoverflow.com/questions/20358654/…
    • @Dr_Dang 好的,您是否尝试过使用 Sub 而不是 OnReadyStateChange 事件的函数?
    猜你喜欢
    • 2012-01-22
    • 1970-01-01
    • 1970-01-01
    • 2016-06-13
    • 2020-02-03
    • 1970-01-01
    • 1970-01-01
    • 2016-05-07
    • 2022-09-19
    相关资源
    最近更新 更多