Classic ASP: Write Binary PDF to File

The following code returns an error.  I think the problem might be related to the fact that I am trying to retrieve and write a binary PDF file.  No error is returned when I use this code to retrieve and write a text file.
<%

 url = "https://www.cl.cam.ac.uk/~mgk25/ucs/UTF-8-Plan9-paper.pdf" 
 set pdfhttp = CreateObject("MSXML2.ServerXMLHTTP") 
 pdfhttp.open "POST", url, false 
 pdfhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded" 
 dim pdata
 pdat = "x=1&y=2&z=3" 
 pdfhttp.send pdat

 set oFs = server.createobject("Scripting.FileSystemObject")
 set oTextFile = oFs.OpenTextFile(Server.MapPath(".") & "\Test.pdf", 2, True)
 oTextFile.Write pdfhttp.responseText
 oTextFile.Close
 set oTextFile = nothing
 set oFS = nothing

 Response.Write(pdfhttp.responseText)
 Response.End

%>

Open in new window

LVL 10
skijAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

zc2Commented:
You can not save a binary file (PDF) as a text file. In fact, FSO does not work with binary data at all.
I'd suggest to use the ADO object instead. Here's a sample:
' your http connection code here

' ado constants
const adTypeBinary = 1
const adTypeText = 2
const adModeRead = 1
const adModeWrite = 2
const adModeReadWrite = 3
const adSaveCreateOverWrite = 2

set ostream = CreateObject("ADODB.Stream")
ostream.Type = adTypeBinary
ostream.Mode = adModeReadWrite
ostream.Open
ostream.Write pdfhttp.responseBody ' -- note, I used responseBody instead of responseText
ostream.Position = 0
ostream.SaveToFile Server.MapPath(".") & "\Test.pdf", adSaveCreateOverWrite
ostream.Close

' rest of your code here

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
skijAuthor Commented:
Thanks!  Also, thank you for pointing out that responseBody should be used.
zc2Commented:
You're welcome!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP

From novice to tech pro — start learning today.