Asynchronous HttpRequest using WinHttp.WinHttpRequest.5.1 in ASP

血红的双手。 提交于 2020-01-03 07:33:08

问题


I was trying to make LINK FINDER and facing 2 issue

Issue 1 (Resolved) :: Unable to get url of redirected page

This was resolved REFERNCE LINK by using WinHttp.WinHttpRequest.5.1

Issue 2 (Unsolved) :: unable to use WinHttp.WinHttpRequest.5.1 object EVENTS Or no callback to asynchronous request

Synchronous request code

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

This is working fine but if I have multuple request , then its taking to much time.

I have tried following Asynchronous request code but get error

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  

Code 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

ERROR - The remote server machine does not exist or is unavailable: 'CreateObject'

Code 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

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

Code 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

In microsoft documentation, they have referred WinHttp.WinHttpRequest.5.1 have 4 event.

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

But i didn't got example of how to use this event, nor i am able to use these event in ASP.

Hope for quick response...


回答1:


have you tried using a Sub instead of a function for that "req_OnReadyStateChange"?

by the way i am using the MSXML2.ServerXMLHTTP object and this is working fine. is there any reason why you are using this WinHttp API?

example with 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
%>


来源:https://stackoverflow.com/questions/20552183/asynchronous-httprequest-using-winhttp-winhttprequest-5-1-in-asp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!