如何在脚本中获取网页内容或检查网址是否可以访问
创建了一个实例 MSXML2.XMLHTTP,该实例是一个 COM 对象,它为客户机与 HTTP 服务器的交互提供了一种方法。然后我们调用 Open 方法,向其传递三个参数:GET(指从服务器检索信息)、strURL(保存"脚本中心"主页 URL 的变量)和 FALSE(使调用同步)。之后,我们使用 Send 方法将我们的请求发送到 HTTP 服务器。
那么我们如何知道
http://www.microsoft.com/technet/scriptcenter/default.mspx
是否可访问呢?只需检查 statusText 属性的值即可。如果 statusText 是 OK,则页面可访问;如果不是 OK,则说明某些地方出错了,我们无法访问指定的 URL。
On Error Resume Next
Const ForWriting = 2
strURL="http://www.microsoft.com/technet/scriptcenter/default.mspx"
Set objHTTP = CreateObject("MSXML2.XMLHTTP")
objHTTP.Open "GET", strURL, FALSE
objHTTP.Send
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile("script_center.htm", ForWriting)
objFile.Write objHTTP.ResponseText
objFile.Close
另外一个下载二进制文件的代码:
Sub GetAHK()
'Get Script Folder Name
Dim objFileSys, strFolder
Set objFileSys = CreateObject("Scripting.FileSystemObject")
strFolder = objFileSys.GetParentFolderName(WScript.ScriptFullName) + "\"
'Download Zipped AHK
Dim objSrvHTTP
Set objSrvHTTP = CreateObject("Msxml2.ServerXMLHTTP.6.0")
Call objSrvHTTP.Open("GET", strURL, False)
objSrvHTTP.Send
if objSrvHTTP.status >= 400 then
MsgBox strMsg3, 16, strCap
WScript.Timeout=1
Exit Sub
end if
'Save to File
Dim objStream
Set objStream = CreateObject("ADODB.Stream")
objStream.Open
objStream.Type = 1
objStream.Write objSrvHTTP.responseBody
objStream.SaveToFile strZip, 2
objStream.Close
Set objSrvHTTP = Nothing
Set objStream = Nothing
'UnZip
Dim objApp, objZip
Set objApp = CreateObject("Shell.Application")
Set objZip = objApp.NameSpace(strFolder + strZip)
objApp.Namespace(strFolder).CopyHere(objZip.items)
objFileSys.DeleteFile(strFolder + strZip)
Set objApp = Nothing
Set objZip = Nothing
'Rename AutoHotkey.exe to MouseGestureL.exe
Dim objFile
Set objFile = objFileSys.GetFile(strFolder + strAHK)
objFile.Name = strMGL
Set objFileSys = Nothing
Set objFile = Nothing
'Run AHK
dim objWShell
Set objWShell = WScript.CreateObject("WScript.Shell")
objWShell.Run(strMGL)
Set objWShell = Nothing
WScript.Timeout=1
WScript.Sleep 1200
End Sub