VB XMLHTTP 异步调用

Set XmlHttp2 = CreateObject("Msxml2.XMLHTTP")
XmlHttp2.Open "GET", Url, True  
XmlHttp2.setRequestHeader "CONTENT-TYPE", "application/json;charset=utf-8"
XmlHttp2.send PostMsg

1、使用MSXML2.XMLHTTP open中参数设置为True

2、onreadystatechange 进行设置

方式有:1)使用Timer

              2)WithEvents声明的DomDocument对象load Xml

              3)自定义类,新建类方法来处理事件,然后把类赋给XMLHTTP对象的OnReadyStateChange事件

3、详细内容参考MSDN

https://docs.microsoft.com/en-us/previous-versions/windows/desktop/ms757030(v=vs.85)

Use the onReadyStateChange Property (Visual Basic)

  • 10/27/2016
  • 4 minutes to read

This topic discusses the implementation details necessary to use onreadystatechange notification in Microsoft Visual Basic applications.

Background Information for onReadyStateChange events

The onreadystatechange callback function was not implemented as a COM automation event in the IXMLHTTPRequest and IServerXMLHTTPRequest components. This is because these components are heavily used in scripting environments, many of which do not support COM events. The onreadystatechange callback function was intended to be easy to use when working with scripting clients such as VBScript and JScript.

Because the onreadystatechange property was not implemented through COM-based automation events, Visual Basic (and C/C++) applications need to implement this callback functionality differently.

Using onReadyStateChange in Visual Basic applications

In Visual Basic, you can use any of the following approaches to design applications that support onreadystatechange events.

  • Use a timer control to poll the readyState property. When the value of the readyState property indicates that the data is ready, turn the timer off.

  • Use a DomDocument object to load the XML and handle the state using the WithEvents keyword.

     Note

    If you are using the IXMLHTTPRequest and IServerXMLHTTPRequest components to first post your XML data to a Web server, this option will not work for you.

  • Create a wrapper class, and create a procedure to handle the event within the class module. Set the procedure to the default, and bind the class to the onreadystatechange event to either the IXMLHTTPRequest or IServerXMLHTTPRequest component, depending on which component you are using with your application.

The following sample application demonstrates each of these three approaches.

To use OnReadyStateChange in a Visual Basic application

  1. Open Microsoft® Visual Basic® 6.0. In the New Project dialog box, double-click Standard EXE.

  2. On the Project menu, click References.

  3. In the Available References list, select Microsoft XML,v6.0, and then click OK.

  4. Add four command buttons to Form1 and set the caption of each button as follows:

    TABLE 1
    ControlCaption
    Command1Fail
    Command2Polling using Timer
    Command3Using Class Wrapper
    Command4Using DOMDocument
  5. Add a timer control to Form1.

  6. Copy and paste the following code into Form1.

    VBCopy

    Option Explicit
    
    Public XMLHttpRequest As MSXML2.XMLHTTP60
    Public WithEvents XMLDom As MSXML2.DOMDocument30
    
    Private Function FunctionReadyStateChange()
        Debug.Print XMLHttpRequest.readyState
    End Function
    
    Private Sub Command1_Click()
        FailedOnReadyState
    End Sub
    
    Private Sub Command2_Click()
        TimerResolution
    End Sub
    
    Private Sub Command3_Click()
        ClassResolution
    End Sub
    
    Private Sub Command4_Click()
        DOMResolution
    End Sub
    
    Private Sub FailedOnReadyState()
    On Error GoTo FailedState
        If Not XMLHttpRequest Is Nothing Then Set XMLHttpRequest = Nothing
    
        Set XMLHttpRequest = New MSXML2.XMLHTTP60
    
        ' Assign the wrapper class object to onreadystatechange.
        XMLHttpRequest.OnReadyStateChange = FunctionReadyStateChange
    
        ' Get some stuff asynchronously.
        XMLHttpRequest.open "GET", "http://localhost/test.xml", True
        XMLHttpRequest.send
    
        Exit Sub
    
    FailedState:
        MsgBox Err.Number & ": " & Err.Description
    
    End Sub
    
    Private Sub TimerResolution()
        If Not XMLHttpRequest Is Nothing Then Set XMLHttpRequest = Nothing
        Timer1.Interval = 1
    
        Set XMLHttpRequest = New MSXML2.XMLHTTP60
    
        ' Get some stuff asynchronously.
        XMLHttpRequest.open "GET", "http://localhost/test.xml", True
        XMLHttpRequest.send
    End Sub
    
    Private Sub ClassResolution()
        If Not XMLHttpRequest Is Nothing Then Set XMLHttpRequest = Nothing
    
        Dim MyOnReadyStateWrapper As MyReadyStateHandler
        Set XMLHttpRequest = New MSXML2.XMLHTTP60
    
        ' Create an instance of the wrapper class.
        Set MyOnReadyStateWrapper = New MyReadyStateHandler
    
        ' Assign the wrapper class object to onreadystatechange.
        XMLHttpRequest.OnReadyStateChange = MyOnReadyStateWrapper
    
        ' Get some stuff asynchronously.
        XMLHttpRequest.open "GET", "http://localhost/test.xml", True
        XMLHttpRequest.send
    
    End Sub
    
    Private Sub DOMResolution()
        If Not XMLHttpRequest Is Nothing Then Set XMLHttpRequest = Nothing
        If Not XMLDom Is Nothing Then Set XMLDom = Nothing
    
        Set XMLDom = New MSXML2.DOMDocument30
    
        XMLDom.async = True
        XMLDom.Load "http://localhost/test.xml"
    
    End Sub
    
    Private Sub Timer1_Timer()
        Debug.Print XMLHttpRequest.readyState
        If XMLHttpRequest.readyState = 4 Then
            MsgBox "Done"
            Timer1.Interval = 0
        End If
    End Sub
    
    Private Sub XMLDom_onreadystatechange()
        Debug.Print XMLDom.readyState
        If XMLDom.readyState = 4 Then
            MsgBox "Done"
        End If
    End Sub
    
  7. From the Project menu, click Add Class Module.

  8. Change the name of the new class module from "Class1" to "MyReadyStateHandler"

  9. Paste the following code into the class module:

    VBCopy

    Option Explicit
    
    Sub OnReadyStateChange()
        Debug.Print Form1.XMLHttpRequest.readyState
        If Form1.XMLHttpRequest.readyState = 4 Then
            MsgBox "Done"
        End If
    End Sub
    
  10. In the sample code added in the previous step, highlight the procedure name "OnReadyStateChange" by selecting it in the Code window.

  11. From the Tools menu, click Procedure Attributes.

    In the Procedure Attributes dialog, the Name combo box should show "OnReadyStateChange."

  12. Click Advanced.

  13. In Procedure ID, select "(Default)" from the available options.

  14. Click OK.

  15. Save the class module (MyReadyStateHandler.cls) to file.

  16. Open Notepad and paste the following XML into it

    XMLCopy

    <?xml version="1.0"?>
    <Root>
      <Testing>This is to test the onreadystatechange event on the XMLHTTPRequest or DOMDocument</Testing>
      <Testing>This is due to the event not being declared in the type library</Testing>
    </Root>
    
  17. Save the file as test.xml to your IIS localhost directory. For example, this folder might be C:\Inetpub\wwwroot for a typical default installation of IIS with Windows 2000.

  18. In Visual Basic, from the Run menu, click Start to run the application.

  19. Try the following command options to observe the different approaches to using the onreadystatechange event within Visual Basic.

    1. To force a ready state failure, click Fail.

    2. To view the polling resolution, click Polling using Timer.

    3. To view the wrapper class solution, click Using Class Wrapper.

    4. To view the DOMDocument approach, click Using DomDocument.

  20. For each of the code paths in the previous step, you can place brake-points at various places to step through the code.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值