| 特性 | Msxml2.XMLHTTP | Msxml2.ServerXMLHTTP |
|---|---|---|
| 快取 | **有** (預設啟用) | **無** |
| 適用端 | 客戶端 (瀏覽器/桌面) | 伺服器端 (ASP/COM+) |
| Cookie/Session 自動處理 | 是 | 否 (需手動) |
| 執行緒安全 | 單執行緒單元 | 多執行緒安全 |
' 登入並取得受保護頁面 - XMLHTTP 自動處理 Cookie
Set xhr = CreateObject("Msxml2.XMLHTTP")
xhr.open "POST", "https://site.com/login", False
xhr.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
xhr.send "user=admin&pass=123456"
xhr.open "GET", "https://site.com/secret", False
xhr.send
WScript.Echo xhr.responseText
' 同一腳本換 ServerXMLHTTP 需手動管理 Cookie
Set sxhr = CreateObject("Msxml2.ServerXMLHTTP")
sxhr.open "POST", "https://site.com/login", False
' ... 需自行解析 Set-Cookie 並加入後續請求 Header
寫 VBS 暴力破解腳本時,若用 XMLHTTP,首次登入成功後快取了 Session,後續嘗試不同密碼卻仍顯示「已登入」,導致判斷邏輯失效。解法:改用 ServerXMLHTTP 或每次請求前清除快取 (無標準 API,建議換物件)。
客戶端要狀態、要快取 → XMLHTTP;
伺服器端要乾淨、要併發 → ServerXMLHTTP。