ZetCode

VBScript readyState 属性

最后修改于 2025 年 4 月 9 日

VBScript 中的 readyState 属性与 XMLHTTP 对象一起使用,用于跟踪 HTTP 请求的状态。它返回一个数字值,指示请求的当前状态。当您需要监控请求进度时,此属性对于异步操作至关重要。

readyState 有五个可能的值,从 0 到 4,每个值代表一个不同的阶段。它通常与 onreadystatechange 事件处理程序一起使用。本教程通过实际示例涵盖 readyState 的用法。

readyState 属性概述

readyState 属性指示 XMLHTTP 请求的当前状态。值范围从 0 (UNSENT) 到 4 (DONE)。每个值代表请求生命周期中的特定阶段。理解这些状态对于正确处理请求至关重要。

状态 0 表示请求未初始化。状态 1 表示连接已建立。状态 2 表示请求已收到。状态 3 表示正在处理。状态 4 表示操作已完成。这些状态有助于跟踪异步请求进度。

基本 readyState 监控

此示例演示了 readyState 属性的基本监控。它展示了如何在 HTTP 请求期间检查状态。脚本发出简单的 GET 请求并记录状态更改。

basic_readystate.vbs
Set xmlhttp = CreateObject("MSXML2.XMLHTTP")
xmlhttp.Open "GET", "https://example.com", False
xmlhttp.Send

WScript.Echo "ReadyState: " & xmlhttp.readyState
WScript.Echo "Status: " & xmlhttp.status

Set xmlhttp = Nothing

脚本创建一个 XMLHTTP 对象并发出同步请求。发送请求后会检查 readyState。对于同步请求,当执行继续时,状态将为 4(完成)。

带状态跟踪的异步请求

此示例展示了如何跟踪异步请求中的 readyState 更改。它使用 onreadystatechange 事件来监控进度。每次状态更改都会触发事件处理程序。

async_readystate.vbs
Set xmlhttp = CreateObject("MSXML2.XMLHTTP")

Sub OnReadyStateChange
    WScript.Echo "State changed to: " & xmlhttp.readyState
    If xmlhttp.readyState = 4 Then
        WScript.Echo "Request completed with status: " & xmlhttp.status
    End If
End Sub

xmlhttp.onreadystatechange = GetRef("OnReadyStateChange")
xmlhttp.Open "GET", "https://example.com", True
xmlhttp.Send

' Keep script running while request completes
Do While xmlhttp.readyState <> 4
    WScript.Sleep 100
Loop

Set xmlhttp = Nothing

脚本创建一个异步请求并定义一个事件处理程序。处理程序会记录每次状态更改。循环确保脚本等待完成。这种模式对于异步操作很常见。

处理不同的 readyState 值

此示例演示了如何处理所有可能的 readyState 值。它展示了如何根据当前状态执行不同的操作。每个状态都会触发特定的处理。

state_handling.vbs
Set xmlhttp = CreateObject("MSXML2.XMLHTTP")

Sub StateHandler
    Select Case xmlhttp.readyState
        Case 0: WScript.Echo "Request not initialized"
        Case 1: WScript.Echo "Server connection established"
        Case 2: WScript.Echo "Request received"
        Case 3: WScript.Echo "Processing request"
        Case 4: 
            WScript.Echo "Request finished"
            If xmlhttp.status = 200 Then
                WScript.Echo "Response: " & xmlhttp.responseText
            End If
    End Select
End Sub

xmlhttp.onreadystatechange = GetRef("StateHandler")
xmlhttp.Open "GET", "https://example.com", True
xmlhttp.Send

Do While xmlhttp.readyState <> 4
    WScript.Sleep 100
Loop

Set xmlhttp = Nothing

脚本使用 Select Case 结构来处理每个状态。状态 4 包括额外的状态检查。这种方法为请求处理提供了精细控制。它对于复杂的操作很有用。

使用 readyState 进行错误处理

此示例演示了如何使用 readyState 和状态码进行错误处理。它展示了如何检测和响应请求失败。正确的错误处理可以使脚本更加健壮。

error_handling.vbs
Set xmlhttp = CreateObject("MSXML2.XMLHTTP")

Sub ErrorHandler
    If xmlhttp.readyState = 4 Then
        If xmlhttp.status = 200 Then
            WScript.Echo "Success: " & xmlhttp.responseText
        Else
            WScript.Echo "Error: " & xmlhttp.status & " - " & xmlhttp.statusText
        End If
    End If
End Sub

xmlhttp.onreadystatechange = GetRef("ErrorHandler")
xmlhttp.Open "GET", "https://example.com/nonexistent", True
xmlhttp.Send

Do While xmlhttp.readyState <> 4
    WScript.Sleep 100
Loop

Set xmlhttp = Nothing

脚本同时检查 readyStatestatus 以进行错误处理。仅当请求完成(状态 4)时,它才会评估状态。这种模式可确保在请求完成后正确检测错误。

带进度跟踪的文件下载

此示例展示了如何使用 readyState 来跟踪文件下载进度。它演示了状态 3 期间的中间处理。脚本在报告进度的同时下载文件。

download_progress.vbs
Set xmlhttp = CreateObject("MSXML2.XMLHTTP")

Sub ProgressHandler
    If xmlhttp.readyState = 3 Then
        WScript.Echo "Download in progress..."
    ElseIf xmlhttp.readyState = 4 Then
        If xmlhttp.status = 200 Then
            Set fso = CreateObject("Scripting.FileSystemObject")
            Set file = fso.CreateTextFile("downloaded_file.html", True)
            file.Write xmlhttp.responseText
            file.Close
            WScript.Echo "File downloaded successfully"
        End If
    End If
End Sub

xmlhttp.onreadystatechange = GetRef("ProgressHandler")
xmlhttp.Open "GET", "https://example.com", True
xmlhttp.Send

Do While xmlhttp.readyState <> 4
    WScript.Sleep 100
Loop

Set xmlhttp = Nothing

脚本使用状态 3 来指示下载进度。完成后(状态 4),它会将响应保存到文件中。这演示了中间状态的实际用途。该方法对于大文件下载效果很好。

来源

XMLHTTP 文档

在本文中,我们探讨了 VBScript 中的 readyState 属性,涵盖了其用法和实际应用。从基本的状态监控到复杂的下载跟踪,这些示例都展示了请求状态管理。有了这些知识,您就可以通过健壮的状态处理来增强您的 HTTP 操作。

作者

我叫 Jan Bodnar,是一位充满激情的程序员,拥有多年的编程经验。我自 2007 年以来一直撰写编程文章。到目前为止,我已撰写了 1400 多篇文章和 8 本电子书。我在编程教学方面拥有八年以上的经验。

列出所有 VBScript 教程