save winHTTPRequest response as an image

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
grimboto
Posts: 53
Joined: 09 Jul 2014, 19:20

save winHTTPRequest response as an image

Post by grimboto » 11 Dec 2019, 15:18

I'm generating a url to send using winHttpRequest that returns Image data but i can't figure out how to save the returned data as a file.

Code: Select all

HTTP := ComObjCreate("WinHttp.WinHttpRequest.5.1") ;Create COM Object


generatedURL := "https://gis.mapshare.vic.gov.au/arcgis/rest/services/mapshare/PropertyAndParcelPOLY/MapServer/export?dpi=96&transparent=true&format=png32&layers=show%3A0%2C1%2C2%2C3&dynamicLayers=%5B%7B%22id%22%3A0%2C%22name%22%3A%22Property%20Proposed%22%2C%22source%22%3A%7B%22type%22%3A%22mapLayer%22%2C%22mapLayerId%22%3A0%7D%2C%22drawingInfo%22%3A%7B%22showLabels%22%3Atrue%7D%2C%22minScale%22%3A30000%2C%22maxScale%22%3A0%7D%2C%7B%22id%22%3A1%2C%22name%22%3A%22Property%22%2C%22source%22%3A%7B%22type%22%3A%22mapLayer%22%2C%22mapLayerId%22%3A1%7D%2C%22drawingInfo%22%3A%7B%22showLabels%22%3Afalse%7D%2C%22minScale%22%3A30000%2C%22maxScale%22%3A0%7D%2C%7B%22id%22%3A2%2C%22name%22%3A%22Parcel%20Proposed%22%2C%22source%22%3A%7B%22type%22%3A%22mapLayer%22%2C%22mapLayerId%22%3A2%7D%2C%22drawingInfo%22%3A%7B%22showLabels%22%3Atrue%7D%2C%22minScale%22%3A30000%2C%22maxScale%22%3A0%7D%2C%7B%22id%22%3A3%2C%22name%22%3A%22Parcel%22%2C%22source%22%3A%7B%22type%22%3A%22mapLayer%22%2C%22mapLayerId%22%3A3%7D%2C%22drawingInfo%22%3A%7B%22showLabels%22%3Afalse%7D%2C%22minScale%22%3A30000%2C%22maxScale%22%3A0%7D%5D&bbox=2523586.1949962866%2C2398402.9073125347%2C2523656.574303712%2C2398440.372387465&bboxSR=3111&imageSR=3111&size=1330%2C708&f=image"



HTTP.Open("GET", generatedURL) ;GET & POST are most frequent, Make sure you UPPERCASE
HTTP.Send() ;If POST request put data in "Payload" variable
Response_data:= HTTP.ResponseText ;Save the text that is returned

msgbox % HTTP.ResponseText
I think i need to use HTTP.ResponseStream but i cant figure out how to get it working.

if you paste the above url into a browser it displays the correct image so i tried UrlDownloadToFile but that didn't work either.

any help would be appreciated

thanks Grimboto
grimboto
Posts: 53
Joined: 09 Jul 2014, 19:20

Re: save winHTTPRequest response as an image

Post by grimboto » 11 Dec 2019, 15:29

Sorry i figured it out

Code: Select all

vStream := Http.ResponseStream

if (ComObjType(vStream) = 0xD) {      ;VT_UNKNOWN = 0xD
	pIStream := ComObjQuery(vStream, "{0000000c-0000-0000-C000-000000000046}")	;defined in ObjIdl.h

	oFile := FileOpen( A_ScriptDIr "\ms-banner.png", "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 "\ms-banner.png")
		run % A_ScriptDIr "\ms-banner.png"
	else
		msgbox File Not Exist
}
i must have missed this the last time i looked it was the first result in google
Post Reply

Return to “Ask for Help (v1)”