I'm having some issues downloading a PDF file from a corporate system. The cookie setup looks OK, and if I select any non-PDF binary from behind the login then the download works fine (e.g: an image) but for one reason or another, downloading the response from a URL that returns a PDF gives me a file which is not complete/fully formed.
The code I've been able to find is below (and which works for images) - can anyone help diagnose the issue?
How can I interpret the responseStream as a PDF stream - or is there an easier way?!
Code: Select all
urltodownload:="http://blah/GetRepositoryContent/CD/Default/Output/tasga_print.pdf/inline/113157975:1/3"
whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
whr.Open("GET", urltodownload,false)
whr.SetRequestHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
whr.SetRequestHeader("Accept-Encoding","gzip, deflate")
whr.SetRequestHeader("Referer", "http://blah/logon/ui/CD/Default/PG/GCCP/Dashboard/AccountingPackByLanguage?AVK=7&SessionTimeout=true&ClientDomain=Default")
whr.SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; Touch; rv:11.0) like Gecko")
whr.SetRequestHeader("Cookie", "JSESSIONID=7E3CB981C25FA4D4D77541E84EEBAE5B; BIGipServerpool_blahgccp.app.blah.net_80=3426989322.20480.0000; CWSessionId=""7E3CB981C25FA4D4D77541E84EEBAE5B""")
whr.SetRequestHeader("Host","blahgccp")
whr.SetRequestHeader("Connection","Keep-Alive")
whr.Send()
whr.WaitForResponse(-1)
vStream := whr.ResponseStream
if (ComObjType(vStream) = 0xD) { ;VT_UNKNOWN = 0xD
pIStream := ComObjQuery(vStream, "{0000000c-0000-0000-C000-000000000046}") ;defined in ObjIdl.h
oFile := FileOpen( A_ScriptDIr "\" saveas, "w")
Loop {
VarSetCapacity(Buffer, 8192)
hResult := DllCall(NumGet(NumGet(pIStream + 0) + 3 * A_PtrSize) ; IStream::Read
, "ptr", pIStream
, "ptr", &Buffer ;pv [out] A pointer to the buffer which the stream data is read into.
, "uint", 8192 ;cb [in] The number of bytes of data to read from the stream object.
, "ptr*", cbRead) ;pcbRead [out] A pointer to a ULONG variable that receives the actual number of bytes read from the stream object.
oFile.RawWrite(&Buffer, cbRead)
} Until (cbRead = 0)
ObjRelease(pIStream)
oFile.Close()
if FileExist(A_ScriptDIr "\" saveas)
run % A_ScriptDIr "\" saveas
else
msgbox File Not Exist
}