How can I download a binary file using Python and WinHTTPRequest?

I need to download a bunch of pdf files from the web. I usually use the urllib3 library, but it is a corporate website with authentication. I can download a normal html web using the following:

url = 'https://corpweb.example/index.html'
h = win32com.client.Dispatch('WinHTTP.WinHTTPRequest.5.1')
h.SetAutoLogonPolicy(0)
h.Open('GET', url, False)
h.Send()
result = h.responseText

But this solution doesn't works with a PDF.

url = "https://corpweb.example/file.pdf"
h = win32com.client.Dispatch('WinHTTP.WinHTTPRequest.5.1')
h.SetAutoLogonPolicy(0)
h.Open('GET', url, False)
h.Send()
with open(filename, 'wb') as f:
    f.write(h.responseText)

I get an error:

TypeError: a bytes-like object is required, not 'str'

What can I do?


Solution 1:

As Microsoft’s documentation of WinHttpRequest explains, responseText contains the response body as Unicode text. To obtain the response body as raw bytes, use responseBody instead.

Also consider using responseStream instead of either, to avoid keeping the entire file in memory at once.