HELP! Download PDF as stream from WinHTTPRequest Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
jaquesy
Posts: 3
Joined: 18 Oct 2021, 07:43

HELP! Download PDF as stream from WinHTTPRequest

Post by jaquesy » 18 Oct 2021, 09:03

Hi there!
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
}

User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

Re: HELP! Download PDF as stream from WinHTTPRequest  Topic is solved

Post by kczx3 » 21 Oct 2021, 20:50

I’d guess it’s because the Accept header doesn’t have the mime-type for pdf files listed.

Post Reply

Return to “Ask for Help (v1)”