我是VBScript的新手(我花了一些时间才知道VBA,VB.Net和VBScript是不同的!)并且正在努力寻找一个看似简单的脚本。
我打算访问一个URL并捕获http状态代码,但我需要首先登录,因为它会将我重定向到登录页面。我可以登录,但我不知道如何导航到我需要的http状态代码的页面。
如何确保登录后,会话对我来说是否有效以访问特定页面?
这是我的代码:
set httpObj = CreateObject("microsoft.xmlhttp")
httpObj.open "POST","http://sampleWebsite/login.asp",false
formValues = "user=username&pass=password"
httpObj.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
httpObj.setRequestHeader "Content-Length", Len(formValues)
httpObj.send formValues
chkStatus = httpObj.status
respTxt=httpObj.responseText
If chkStatus = 200 Then
If (InStr(respTxt,"<title>Sign In")) Then
WScript.Echo "The login failed!"
Else
WScript.Echo "Login was successfull!"
End If
End If
请帮忙。
答案 0 :(得分:0)
解决了!
成功登录后,只需在同一网站上尝试过URL。检查URL是否重定向到登录页面或主页。通过在文本文件中记录responseText并使用页面源验证它来检查这一点。
set httpObj = CreateObject("microsoft.xmlhttp")
httpObj.open "POST","http://sampleWebsite/login.asp",false
formValues = "user=user_name&pass=pass_word"
httpObj.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
httpObj.setRequestHeader "Content-Length", Len(formValues)
httpObj.send formValues
chkStatus = httpObj.status
respTxt=httpObj.responseText
Set objFSO=CreateObject("Scripting.FileSystemObject")
strLogName = "log.txt"
strLogPath = "C:\Users\useracc\Documents\Logs\" & strLogName ' Set log path here.
' Instantiate Logging
Set objLogFile = objFSO.OpenTextFile(strLogPath, 8, 2)
If chkStatus = 200 Then
If (InStr(respTxt,"<font color=red><B>The user ID or password you have entered does not exist.")) Then
WScript.Echo "The login failed!"
WriteLog "#################### Script Log Initialized ####################"
WriteLog respTxt
WriteLog "#################### Script Log Completed ####################"
Else
WScript.Echo "Login was successfull!"
'WScript.Echo respTxt
WriteLog "#################### Script Log Initialized ####################"
WriteLog respTxt
WriteLog "#################### Script Log Completed ####################"
End If
End If
httpObj.open "GET","http://sampleWebsite/home.asp",false
httpObj.send
chkStatus = httpObj.status
respTxt=httpObj.responseText
If chkStatus = 200 Then
If (InStr(respTxt,"Please enter a valid user name and password to continue.<br>")) Then
WScript.Echo "Session is inactive!"
WriteLog "#################### Script Log Initialized ####################"
WriteLog respTxt
WriteLog "#################### Script Log Completed ####################"
Else
WScript.Echo "Session is active!"
'WScript.Echo respTxt
WriteLog "#################### Script Log Initialized ####################"
WriteLog respTxt
WriteLog "#################### Script Log Completed ####################"
End If
End If
Sub WriteLog(sEntry)
'On Error Resume Next
objLogFile.WriteLine(Now() & ": Log: " & sEntry)
End Sub