Re: XMLHTTP download has 404 status
- From: "Michael Harris \(MVP\)" <mikhar at mvps dot org>
- Date: Wed, 3 May 2006 16:48:26 -0700
synapticflame@xxxxxxxxx wrote:
I'm trying to use VBScript to download a zip file from a course
management system. Here's the part that does the downloading:
I used your code with...
call download("http://www.microsoft.com","c:\temp\temp.txt")
....and the returned html was saved where it was expected. Didn't try a
*.zip URL since I don't know of one offhand.
'======================================
' function to download a file from URL sSource to file sDest
function Download(sSource,sDest)
Dim oHttp, Stream, Start, fso, OldName
Const adModeReadWrite = 0, adTypeBinary = 1, adSaveCreateOverwrite = 2
Set fso = CreateObject("Scripting.FileSystemObject")
' If there's already something by that name, rename it with .old
if fso.fileexists(Sdest) then
OldName = Sdest & ".old"
' If there's already a .old, delete it
if fso.fileexists(OldName) then
fso.DeleteFile (OldName)
fso.MoveFile Sdest ,OldName
end if
end if
Set oHttp = CreateObject("MSXML2.XMLHTTP")
oHttp.Open "GET", sSource, False
oHttp.send
' If we can't have it perfect, we need to know.
if oHTTP.status <> 200 then
msgbox "unexpected status: " _
& oHTTP.status & " " & oHTTP.statusText _
& " for " & vbcrlf _
& sSource
wscript.quit
end if
' Ok, write the file
Set Stream = CreateObject("adodb.Stream")
Stream.Type = adTypeBinary
Stream.Mode = adModeReadWrite
Stream.Open
Stream.write oHttp.responseBody
Stream.SaveToFile sDest, adSaveCreateOverwrite
Stream.Close
Set oHttp = Nothing
Set Stream = Nothing
end function
'======================================
I call this function with a given URL and destination. It quits with a
status of 404 File not Found. If I pass the same url to
internetexplorer.navigate, I get a nice Save As dialog which
successfully retrieves the file. The environment from which I am
retrieving this zip file seems to be looking for things like a cookie
or referrer values in the header.
Is there a way I can automate the Save As window?
Is there some other way to save the file using Internet Explorer
instead of MSXML2.XMLHTTP?
--
Michael Harris
Microsoft MVP Scripting
.
- Follow-Ups:
- Re: XMLHTTP download has 404 status
- From: kuqumi
- Re: XMLHTTP download has 404 status
- From: kuqumi
- Re: XMLHTTP download has 404 status
- References:
- XMLHTTP download has 404 status
- From: synapticflame
- XMLHTTP download has 404 status
- Prev by Date: Re: How to copy a file to a system folder???
- Next by Date: Re: why can't I catch errors?
- Previous by thread: XMLHTTP download has 404 status
- Next by thread: Re: XMLHTTP download has 404 status
- Index(es):