MSXML2.ServerXMLHTTP works only with text files?

B

Bob Barrows [MVP]

lopi said:
Hello.
I'm trying to remotely get a pdf file - http://remoteServer/file.pdf -
in order to store it into another server, maybe with
Scripting.FileSystemObject
However the following code doesn't work properly:
------------
url = "http://remoteServer/file.pdf"
set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP")
xmlhttp.open "GET", url, false
xmlhttp.send ""
------------
as xmlhttp.responseText does not contain the whole file textStream,
but only a part of it.
Anyone can help?
TIA

There are three properties that can be used to get the output from xmlhttp:
ResponseText
ResponseXML
ResponseStream

It seems to me you need to use the last one. You should probably use an ADO
Stream object to save it to disk.
 
L

lopi

It seems to me you need to use the last one. You should probably use an ADO
Stream object to save it to disk.

thanks Bob ...
actually i have changed the code in the meanwhile
------------
url = "http://remoteServer/file.pdf"
set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP")
xmlhttp.open "GET", url, false
xmlhttp.send ""
Response.Buffer = TRUE
Response.ContentType = "application/pdf"
response.BinaryWrite xmlhttp.responsestream
------------
but again the browser displays only a part of the pdf file, not as PDF
obviously, but as a sequence of chars (%PDF-1.4 %ÿÿÿÿ 6 0 obj <>
endobj xref 6 13 0000000016 00000 n 0000000719 00000 n 0000000795
00000 n 0000000928 00000 n 0000001048 00000 n 0000002524 00000 n
0000002558 00000 n 0000002777 00000 n 0000002971 00000 n 0000005640
00000 n 0000005867 00000 n 0000006088 00000 n 0000000556 00000 n
trailer <<150F4F63E5FC2F48B1DDB1CB337193D0>]>> startxref 0 %%EOF 18 0
obj<>stream xÿÿ``ÿÿ`ÿÿ`)
 
A

Anthony Jones

It seems to me you need to use the last one. You should probably use an ADO
Stream object to save it to disk.

thanks Bob ...
actually i have changed the code in the meanwhile
------------
url = "http://remoteServer/file.pdf"
set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP")
xmlhttp.open "GET", url, false
xmlhttp.send ""
Response.Buffer = TRUE
Response.ContentType = "application/pdf"
response.BinaryWrite xmlhttp.responsestream
------------
but again the browser displays only a part of the pdf file, not as PDF
obviously, but as a sequence of chars (%PDF-1.4 %ÿÿÿÿ 6 0 obj <>
endobj xref 6 13 0000000016 00000 n 0000000719 00000 n 0000000795
00000 n 0000000928 00000 n 0000001048 00000 n 0000002524 00000 n
0000002558 00000 n 0000002777 00000 n 0000002971 00000 n 0000005640
00000 n 0000005867 00000 n 0000006088 00000 n 0000000556 00000 n
trailer <<150F4F63E5FC2F48B1DDB1CB337193D0>]>> startxref 0 %%EOF 18 0
obj<>stream xÿÿ``ÿÿ`ÿÿ`)
<<<<<<<<<<<<<<<<

I'm surprised you get even that. I wouldn't have thought ResponseStream
would be an acceptable value to pass to BinaryWrite. You should use
ResponseBody.

If you still get the same results change the last couple of lines to:-

Response.contentype = "text/html"
Response.Write LenB(xmlHttp.ResponseBody)

So you can discover the exact size of whats in it. You can therefore
determine which stage of the transfer to concentrate further investigations.
 
L

lopi

I'm surprised you get even that. I wouldn't have thought ResponseStream
would be an acceptable value to pass to BinaryWrite. You should use
ResponseBody.

Thank you very much Anthony, good guess!
I enclose the working code, for anyone may need it.
<%
'Byte string to string conversion
Function getString(byVal StringBin)
dim intCount
getString =""
For intCount = 1 to LenB(StringBin)
getString = getString & chr( AscB(MidB(StringBin, intCount, 1)) )
Next
End Function

url = "http://remoteServer/file.pdf"
set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP")
xmlhttp.open "GET", url, false
xmlhttp.send ""
set fileSys = Server.CreateObject("Scripting.FileSystemObject")
set textStream = fileSys.CreateTextFile(Server.MapPath("/localDir/
file.pdf"), True, False)
textStream.Write getString(xmlhttp.ResponseBody)
textStream.Close
Set textStream = Nothing
Set fileSys = Nothing
%>

Any performance issue?

I'll try tolearn how to POST the file by MSXML2.ServerXMLHTTP, instead
of creating it by Scripting.FileSystemObject, it's likely to be a
better solution.

ciao.lopi
 
A

Anthony Jones

lopi said:
Thank you very much Anthony, good guess!
I enclose the working code, for anyone may need it.
<%
'Byte string to string conversion
Function getString(byVal StringBin)
dim intCount
getString =""
For intCount = 1 to LenB(StringBin)
getString = getString & chr( AscB(MidB(StringBin, intCount, 1)) )
Next
End Function

url = "http://remoteServer/file.pdf"
set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP")
xmlhttp.open "GET", url, false
xmlhttp.send ""
set fileSys = Server.CreateObject("Scripting.FileSystemObject")
set textStream = fileSys.CreateTextFile(Server.MapPath("/localDir/
file.pdf"), True, False)
textStream.Write getString(xmlhttp.ResponseBody)
textStream.Close
Set textStream = Nothing
Set fileSys = Nothing
%>

Any performance issue?

This is easier and puts less strain on your server.

Dim oWinHTTP
Dim oStream

Set oWinHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")

oWinHTTP.Open "GET", "http://remoteServer/file.pdf", False
oWinHTTP.Send

If oWinHTTP.Status = 200 Then
Set oStream = CreateObject("ADODB.Stream")
oStream.Open
oStream.Type = 1
oStream.Write oWinHTTP.responseBody
oStream.SaveToFile Server.MapPath("/localDir/file.pdf")
oStream.Close
End If



I'll try tolearn how to POST the file by MSXML2.ServerXMLHTTP, instead
of creating it by Scripting.FileSystemObject, it's likely to be a
better solution.

I'm not sure what you mean?
 
L

lopi

I'm not sure what you mean?

Something like
-------------------
url = "http://remoteServer/file.pdf"
set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP")
xmlhttp.open "GET", url, false
xmlhttp.send ""
ResponseBody = xmlhttp.ResponseBody
If xmlhttp.Status = 200 Then
url ="http://localServer/localDir/file.pdf"
xmlhttp.open "POST", url, false
xmlhttp.send ResponseBody
End if
Response.Write xmlhttp.Status
-------------------
but I obtain a status=404

anyway, i'm easy with your solution, but what if I have to download a
word file (.doc) instead of PDF?
I guess there's an equivalent to CreateObject("ADODB.Stream"), where
can I find the classes corresponding to main MIME types?
 
A

Anthony Jones

lopi said:
Something like
-------------------
url = "http://remoteServer/file.pdf"
set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP")
xmlhttp.open "GET", url, false
xmlhttp.send ""
ResponseBody = xmlhttp.ResponseBody
If xmlhttp.Status = 200 Then
url ="http://localServer/localDir/file.pdf"
xmlhttp.open "POST", url, false
xmlhttp.send ResponseBody
End if
Response.Write xmlhttp.Status
-------------------
but I obtain a status=404

anyway, i'm easy with your solution, but what if I have to download a
word file (.doc) instead of PDF?
I guess there's an equivalent to CreateObject("ADODB.Stream"), where
can I find the classes corresponding to main MIME types?

The goal posts keep moving.

Am I to believe there are now three servers involved. The server you are
pulling from, a server where this code is running AND a different local
server where you want to place the file??

The code I posted will work for any type of file including Word documents.
 
L

lopi

The goal posts keep moving.

Am I to believe there are now three servers involved. The server you are
pulling from, a server where this code is running AND a different local
server where you want to place the file??

The code I posted will work for any type of file including Word documents.-

You're right, forget it! I was confusing myself.
I tried your code over a word document and it works perfectly.
Thank you again.

ciao.lopi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top