Author Topic: COM Error in Web File Copy Code  (Read 37 times)

limnos

  • Newbie
  • *
  • Posts: 17
COM Error in Web File Copy Code
« on: November 29, 2018, 01:28:06 pm »
Not great at object code work, but with some help getting closer.  Trying to copy down a zip file from a web link and unzip it.  I'm getting a COM oject\Memory error on this line: varByteArray = WinHttpReq.ResponseBody.  Any idea why that is happening and what the fix would be?

strFile = "%localpath%"
; Instantiate a WinHttpRequest object.
WinHttpReq = ObjectCreate("WinHttp.WinHttpRequest.5.1")
; Initialize an HTTP request.
WinHttpReq.Open("GET", "%targetfile%", @FALSE)
; Send the HTTP request.
WinHttpReq.Send()
; Display the response text.
varByteArray = WinHttpReq.ResponseBody
buf = BinaryAllocArray(varByteArray)
data = BinaryWrite(buf, strFile)
BinaryFree(buf); Clean up
WinHttpReq  = 0
timedelay(1)
zUnZipFiles("u o d", "%localpath%", "*.*", "", "%unziploc%")
timedelay(1)

limnos

  • Newbie
  • *
  • Posts: 17
Re: COM Error in Web File Copy Code
« Reply #1 on: November 29, 2018, 01:30:06 pm »
I tried using this code instead, but it still chokes with a COM error on WinHttpReq.ResponseBody

num="0"
strFile = "%localpath%"
; Instantiate a WinHttpRequest object.
WinHttpReq = ObjectCreate("Microsoft.XMLHTTP")
; Initialize an HTTP request.
WinHttpReq.Open("GET", "%targetfile%", @FALSE)
; Send the HTTP request.
WinHttpReq.Send()
num = WinHttpReq.Status
If num == "200" then
   oStream = ObjectCreate("ADODB.Stream")
   oStream.Open
   oStream.Type = 1
   oStream.Write WinHttpReq.ResponseBody
   oStream.SaveToFile(strFile)
   oStream.Close
Endif
timedelay(1)
zUnZipFiles("u o d", "%localpath%", "*.*", "", "%unziploc%")
timedelay(1)

td

  • Tech Support
  • *****
  • Posts: 2653
    • WinBatch
Re: COM Error in Web File Copy Code
« Reply #2 on: November 29, 2018, 02:24:58 pm »
Your problem has more than one possible cause.  It could be that you need to set specific request header information, your file URL is incorrect, or you are attempting to download a massive (gigabytes) file.  Don't know which if any of these it might be.  What exactly is the text of the error including what you see by pressing the "More Error Info" button? 
"Success is a lousy teacher. It seduces smart people into thinking they can't lose."
  - Bill Gates