Getting "Access Denied" while downloading an object - vbscript

I am trying to download a svn client by running a batch script. For that I am using this piece of VBS which I call from the batch file. Now this code works because I have successfully downloaded some files but when I am trying to download this file from sourceforge.net I am getting a access denied error message after send(). Any insight on why this is happening and can be avoided will be helpful.
Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP")
objXMLHTTP.open "GET", strFileURL, false
objXMLHTTP.send()
do until objXMLHTTP.Status = 200 : wscript.sleep(1000) : loop
If objXMLHTTP.Status = 200 Then
Set objADOStream = CreateObject("ADODB.Stream")
objADOStream.Open
objADOStream.Type = 1 'adTypeBinary
objADOStream.Write objXMLHTTP.ResponseBody
objADOStream.Position = 0 'Set the stream position to the start
Set objFSO = Createobject("Scripting.FileSystemObject")
If objFSO.Fileexists(strHDLocation) Then objFSO.DeleteFile strHDLocation
Set objFSO = Nothing
objADOStream.SaveToFile strHDLocation
objADOStream.Close
Set objADOStream = Nothing
End if
Set objXMLHTTP = Nothing

You're getting access denied because you're trying to download a file from a URL that is redirecting you. If you try to download a file directly, you'll find you won't get the error.
You should use the latest version..
Set objXMLHTTP= CreateObject("Msxml2.XMLHttp.6.0")
However using..
Set objXMLHTTP = CreateObject("Microsoft.XMLHTTP")
..is ok for now.
If you were to add sourceforge.com to your trusted sites in IE, go into IE Options->Security, select Trusted sites go into the Custom Level, change "Access data sources across domains" to Enable, that should let you get beyond that Access Denied error.

Related

How Can I Ignore Internet Errors in a VBScript?

I have a vbscript that downloads a file. I need to make it so that if there is no internet that it wont pop up with the error message The operation timed out, or Failed to find the resource specified. I've tried using On Error Resume Next, but alas it does not skip any internet related errors. Any way I can set a timeout or something? It is not a large file, just a 20-line text file. Here is my script:
strFileURL = "https://minecraft-statistic.net/en/server/167.114.43.185_25565/json/"
strHDLocation = "c:\users\public\mc.txt"
Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP")
objXMLHTTP.open "GET", strFileURL, false
objXMLHTTP.send()
If objXMLHTTP.Status = 200 Then
Set objADOStream = CreateObject("ADODB.Stream")
objADOStream.Open
objADOStream.Type = 1 'adTypeBinary
objADOStream.Write objXMLHTTP.ResponseBody
objADOStream.Position = 0 'Set the stream position to the start
Set objFSO = Createobject("Scripting.FileSystemObject")
If objFSO.Fileexists(strHDLocation) Then objFSO.DeleteFile strHDLocation
Set objFSO = Nothing
objADOStream.SaveToFile strHDLocation
objADOStream.Close
Set objADOStream = Nothing
End if
Set objXMLHTTP = Nothing
On Error Resume Next is the only option for capturing errors, I'm not sure why you say it doesn't work. This works for me;
On Error Resume Next
strFileURL = "https://minecraft-statistic.net/en/server/167.114.43.185_25565/json/"
strHDLocation = "c:\users\public\mc.txt"
Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP")
objXMLHTTP.open "GET", strFileURL, false
objXMLHTTP.send()
If Err.Number = 0 Then
If objXMLHTTP.Status = 200 Then
Set objADOStream = CreateObject("ADODB.Stream")
objADOStream.Open
objADOStream.Type = 1 'adTypeBinary
objADOStream.Write objXMLHTTP.ResponseBody
objADOStream.Position = 0 'Set the stream position to the start
Set objFSO = Createobject("Scripting.FileSystemObject")
If objFSO.Fileexists(strHDLocation) Then objFSO.DeleteFile strHDLocation
Set objFSO = Nothing
objADOStream.SaveToFile strHDLocation
objADOStream.Close
Set objADOStream = Nothing
End if
Set objXMLHTTP = Nothing
Else
'Handle the error here.
End If
The way On Error Resume Next works is as follows;
Line triggers an error which is caught by the VBScript runtime.
The error is recorded in the Err object.
The line is skipped and the next statement is run.
This process will continue until an On Error Goto 0 line is reached at which point the default behaviour resumes.
Useful Links
VBScript — Using error handling

Vb-script parse webpages with username and password

NOTE: I need to use something like Vb-Script or batch because that is what i am familiar with and i also need to be able to run, edit, and create files.
I need to name MS Word files values on a webpage. I decided to first download the webpage as HTML using this code:
function download(sFileURL, sLocation, async)
set objXMLHTTP = CreateObject("MSXML2.XMLHTTP")
objXMLHTTP.open "GET", sFileURL, async
on error resume next
objXMLHTTP.send()
if err.number = 0 then
do until objXMLHTTP.Status = 200
wscript.echo objXMLHTTP.Status
wcript.sleep(200)
loop
if objXMLHTTP.Status = 200 Then
set objADOStream = CreateObject("ADODB.Stream")
objADOStream.Open
objADOStream.Type = 1
objADOStream.Write objXMLHTTP.ResponseBody
objADOStream.Position = 0
set objFSO = Createobject("Scripting.FileSystemObject")
If objFSO.Fileexists(sLocation) Then objFSO.DeleteFile sLocation
Set objFSO = Nothing
objADOStream.SaveToFile sLocation
objADOStream.Close
set objADOStream = Nothing
download = true
end if
else
download = false
end if
set objXMLHTTP = Nothing
end function
if download("https://web.example.com\login.php", "C:\", false) then
wscript.echo "download ok"
else
wscript.echo "download nok"
end if
The only problem is that to view this page you need to be logged in with a username and password.
NOTE: I can't use the actual webpage for security reasons.

Download zip file on site that requires authentication using VBScript

I'm trying to download a ZIP file using VBScript that requires authentication. If you go to the site you'll notice it pops up an authentication prompt. The problem I have is after this script runs the ZIP file is too small for what it should be and is corrupt so I can't open it.
My thought is the download isn't working.
Anyone see what I'm doing wrong?
strHDLocation = "C:\Test\file1.zip"
Set xmlHttp = CreateObject("Microsoft.XMLHTTP")
xmlHttp.Open "GET", "http:downloadsite/report-id=123456", False, "myidhere", "mypwhere"
xmlHttp.Send()
Set objADOStream = CreateObject("ADODB.Stream")
objADOStream.Open
objADOStream.Type = 1 'adTypeBinary
objADOStream.Write xmlHttp.ResponseBody
objADOStream.Position = 0 'Set the stream position to the start
Set objFSO = Createobject("Scripting.FileSystemObject")
If objFSO.Fileexists(strHDLocation) Then objFSO.DeleteFile strHDLocation
Set objFSO = Nothing
objADOStream.SaveToFile strHDLocation
objADOStream.Close
Set objADOStream = Nothing
As a bare minimum when using IXmlHttpRequest you should check the Status property to make sure that assumptions are not made about what is being returned.
If xmlHttp.Status = 200 Then
'Successful Request
Else
'Something went wrong
End If
It's likely the request has failed for one reason or another and the ResponseBody contains the failed response not the expected ZIP file.

vbscript downloading file from cache

I need to download latest file from website without using wget or any external utility. I found following vbscript on stack overflow itself (didnt know link, because I have cleared the cache and history)
' Set your settings
strFileURL = "http://somewebsitehere.com/somefile"
strHDLocation = "D:\somepath\"
' Fetch the file
Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP")
objXMLHTTP.open "GET", strFileURL, false
objXMLHTTP.send()
If objXMLHTTP.Status = 200 Then
Set objADOStream = CreateObject("ADODB.Stream")
objADOStream.Open
objADOStream.Type = 1 'adTypeBinary
objADOStream.Write objXMLHTTP.ResponseBody
objADOStream.Position = 0
'Set the stream position to the start
Set objFSO = Createobject("Scripting.FileSystemObject")
If objFSO.Fileexists(strHDLocation) Then objFSO.DeleteFile strHDLocation
Set objFSO = Nothing
objADOStream.SaveToFile strHDLocation
objADOStream.Close
Set objADOStream = Nothing
End if
Set objXMLHTTP = Nothing
However after using this script, I found it did work correctly for first few trials but later download same file from cache instead of downloading the fresh file from net. I get same file from C:\Users\username\AppData\Local\Microsoft\Windows\INetCache\IE\0XQSU247.
I need to clear the INETCache and then re run the script to get the fresh file.
So how should this script be modified so it gets latest updated file from web server instead of getting files from cache.
This question might be lame, but I have no knowledge of vbscript.

How do I use %SYSTEMROOT% in VBS?

I use this code to download a link and I want to save a file in %SystemRoot%\system32 but when replace C:\ with %SystemRoot%, the VBS can't run.
function download(sFileURL, sLocation)
'create xmlhttp object
Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP")
'get the remote file
objXMLHTTP.open "GET", sFileURL, false
'send the request
objXMLHTTP.send()
'wait until the data has downloaded successfully
do until objXMLHTTP.Status = 200 : wcript.sleep(1000) : loop
'if the data has downloaded sucessfully
If objXMLHTTP.Status = 200 Then
'create binary stream object
Set objADOStream = CreateObject("ADODB.Stream")
objADOStream.Open
'adTypeBinary
objADOStream.Type = 1
objADOStream.Write objXMLHTTP.ResponseBody
'Set the stream position to the start
objADOStream.Position = 0
'create file system object to allow the script to check for an existing file
Set objFSO = Createobject("Scripting.FileSystemObject")
'check if the file exists, if it exists then delete it
If objFSO.Fileexists(sLocation) Then objFSO.DeleteFile sLocation
'destroy file system object
Set objFSO = Nothing
'save the ado stream to a file
objADOStream.SaveToFile sLocation
'close the ado stream
objADOStream.Close
'destroy the ado stream object
Set objADOStream = Nothing
'end object downloaded successfully
End if
'destroy xml http object
Set objXMLHTTP = Nothing
End function
download "http://remote-location-of-file", "C:\name-of-file-and-extension"
download "http://demo.com/test.gif", "C:\test.gif" **OK**
download "http://demo.com/test.gif", "%SystemRoot%\system32\test.gif" **Faild**
You need to read environment variable in a VBS variable. Something like:
Dim mySystemRoot
Dim myPath
Dim wshShell
Set wshShell = CreateObject( "WScript.Shell" )
mySystemRoot = wshShell.ExpandEnvironmentStrings( "%SystemRoot%" )
wshShell = Nothing
download "http://demo.com/test.gif", myPath & "\test.gif"
Then use mySystemRoot variable to form your file path.

Resources