我需要从运行在Windows Server 2012计算机上的vbscript文件中调用url。脚本和域URL位于同一服务器上。我尝试使用的代码片段导致了一个我无法看到的错误。这个片段是否正确?我是否需要在某处包含Microsoft.XMLHTTP库? vbscript文件与' .asp'一起保存。延期。这两个Dim语句可以进入Sub?
Dim objRequest
Dim URL
Set objRequest = CreateObject(“Microsoft.XMLHTTP”)
URL = “http://example.com/api/Account/List”
objRequest.open “POST”, URL , false
objRequest.Send
Set objRequest = Nothing
以下是我尝试使用此代码段的完整函数调用。我把剪断放在End Sub行之前的末尾。
Sub addUser(uname, pword, resId)
If Not FSO.FileExists(PASSWORD_FILE) Then
Call FileLock("newpass", true)
On Error Resume Next
Set passtxt = FSO.OpenTextFile(PASSWORD_FILE, ForAppend, True)
If (Err) Then
Response.Write BAD_PERMISSIONS
Exit Sub
End If
passtxt.Close
Call FileLock("newpass", false)
End If
If UserExists(uname) Then
If (writeLog("bad","ADD-" & DUPLICATE_USER & "|" & GetTimeStamp(Now) & "|" & uname & "|" & resId & "|" & IP_ADDRESS)) Then
Response.Write DUPLICATE_USER
Response.End
Else
Response.Write BAD_PERMISSIONS
Response.End
End If
End If
Call FileLock("pass", true)
On Error Resume Next
Set passtxt = FSO.OpenTextFile(PASSWORD_FILE, ForAppend, True)
If (Err) Then
Response.Write BAD_PERMISSIONS
Exit Sub
End If
passtxt.WriteLine(uname & ":" & pword)
passtxt.Close
Call FileLock("pass", false)
Call FileLock("log", true)
On Error Resume Next
Set logtxt = FSO.OpenTextFile(ACTION_LOG_FILE, ForAppend, True)
If (Err) Then
Response.Write BAD_PERMISSIONS
Exit Sub
End If
logtxt.WriteLine("ADD-" & SUCCESS & "|" & GetTimeStamp(Now) & "|" & uname & "|" & resId & "|" & IP_ADDRESS)
logtxt.Close
Call FileLock("log", false)
Response.Write SUCCESS
End Sub
答案 0 :(得分:1)
请注意此处的错误处理。我正在测试COM错误和服务器响应
Sub HttpGet
On Error Resume Next
Set File = WScript.CreateObject("Microsoft.XMLHTTP")
File.Open "GET", Arg(1), False
File.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 1.1.4322; .NET CLR 3.5.30729; .NET CLR 3.0.30618; .NET4.0C; .NET4.0E; BCD2000; BCD2000)"
File.Send
txt=File.ResponseText
'Putting in line endings
Outp.write txt
If err.number <> 0 then
Outp.writeline ""
Outp.writeline "Error getting file"
Outp.writeline "=================="
Outp.writeline ""
Outp.writeline "Error " & err.number & "(0x" & hex(err.number) & ") " & err.description
Outp.writeline "Source " & err.source
Outp.writeline ""
Outp.writeline "HTTP Error " & File.Status & " " & File.StatusText
Outp.writeline File.getAllResponseHeaders
Outp.writeline Arg(1)
End If
End Sub