0

Hey I've been trying for hours to receive a XML via a classic ASP page but it just wont work.

Here my code:

post.asp

url = "http://myurl.com/receive.asp"
information = "<Send><UserName>Colt</UserName><PassWord>Taylor</PassWord><Data>100</Data></Send>"
Set xmlhttp = server.Createobject("MSXML2.ServerXMLHTTP")
xmlhttp.Open "POST", url, false
xmlhttp.setRequestHeader "Content-Type", "text/xml" 
xmlhttp.send information

receive.asp

Dim objXmlRequest
Set objXmlRequest = Server.CreateObject("MSXML2.DOMDOCUMENT.3.0")
  objXmlRequest.async = False
  objXmlRequest.setProperty "ServerHTTPRequest", True
  objXmlRequest.validateOnParse = True
  objXmlRequest.preserveWhiteSpace = True

IF objXmlRequest.Load (Request) THEN
  'GET THE REQUEST FROM CLIENT
  strQuery = "//" & "ActionName"
  Set oNode = objXmlRequest.selectSingleNode(strQuery)
  strActionName = oNode.Text
  response.write("success")
ELSE
    Response.Write "Failed to load XML file, reason: " & objXmlRequest.parseError.reason 
END IF

This error appears:

Failed to load XML file, reason: XML document must have a top level element.

I just don't get it. Also i want to know if there is a posibilty to save the loaded XML?

user692942
  • 16,398
  • 7
  • 76
  • 175
NyBu
  • 11
  • 1
  • 1
  • 2
  • Please confirm once if your XML has root tag.. – BabyDuck Mar 30 '15 at 08:58
  • "Post.asp" is only posting data. How do you see the response of "receive.asp"? – Ali Sheikhpour Mar 30 '15 at 09:02
  • @ Ali Sheikhpour - what do you mean with "How do you see the response of receive.asp"? – NyBu Mar 30 '15 at 10:08
  • @NyBu This looks decidedly similar to this question from May 2010 - [How to Send and Receive XML request to another ASP classic page?](http://stackoverflow.com/questions/2784759/how-to-send-and-receive-xml-request-to-another-asp-classic-page) – user692942 Mar 30 '15 at 10:53
  • @NyBu Also this from Dec 2008 - [receiving xml from another website's call to ServerXMLHTTP post in classic asp](http://stackoverflow.com/questions/369226/receiving-xml-from-another-websites-call-to-serverxmlhttp-post-in-classic-asp) is there a pattern to these questions why you sending credentials in XML in the first place? – user692942 Mar 30 '15 at 10:59
  • I made my code with the help of those two questions... but still, it doasn't work and i don't get what is wrong with it. – NyBu Mar 30 '15 at 11:07
  • possible duplicate of [error: XML document must have a top level element](http://stackoverflow.com/questions/21278513/error-xml-document-must-have-a-top-level-element) – user692942 Mar 30 '15 at 11:12
  • @NyBu Have you tried with `objXmlRequest.preserveWhiteSpace = False`? – user692942 Mar 30 '15 at 11:14
  • @Lankymart - yep, tried it, but doasn't work. – NyBu Mar 30 '15 at 11:51
  • I have run your original code with a slight modifications: 1) to post.asp, just to response.write xmlhttp.responseText, and 2) to receive.asp to change the query to "//UserName" and it works fine if you open the page post.asp – JohnRC Mar 30 '15 at 13:20
  • What do you mean with "just to response.write xmlhttp.responseText," ? – NyBu Mar 30 '15 at 13:32
  • @NyBu, sorry I was not clear - I have posted the full code in my second answer below – JohnRC Mar 30 '15 at 13:47

2 Answers2

0

Try this

Post.asp

<%
    information = "<Send><UserName>Colt</UserName><PassWord>Taylor</PassWord><Data>100</Data></Send>"
    Response.ContentType = "text/xml"
    Response.Write information
    Response.End
%>

Receive.asp

<%
    '// URL from which to get the XML - it has to include the full URL, I think
    Dim URL: URL = "http://localhost:8096//pages/test/post.asp"

    response.write URL & "<br/>"


    '// Request the XML from the post.asp page
    Set xmlhttp = server.Createobject("MSXML2.ServerXMLHTTP")
    xmlhttp.Open "GET", url, false
    xmlhttp.send

    '// Get the received XML object directly from the xmlhttp object
    dim objXML: set objXML = xmlHttp.ResponseXML
    response.write typename(objXML) & "<br/>"

    '// Check that XML has been recieved
    IF not objXML is Nothing THEN

        '// Get the data from the xml
        strQuery = "//UserName"
        Set oNode = objXML.selectSingleNode(strQuery)
        strUserName = oNode.Text
        response.write "success - user = " & strUserName & "<br/>"

    ELSE

        Response.Write "Failed to load XML file"

    END IF

%>

This is the result when you open the page "receive.asp" (in my case http://localhost:8096/pages/test/receive.asp )

http://localhost:8096/pages/test/post.asp
DOMDocument
success - user = Colt
JohnRC
  • 1,251
  • 1
  • 11
  • 12
  • Tried it... doasn't work :/ The receive.asp takes minutes to load and after loading it only shows the URL of the post.asp... – NyBu Mar 30 '15 at 12:14
  • It's pretty much instant on my server. Are you using a full version of IIS? I think the IIS version that comes packaged with windows (not server versions) does not allow more than one request to be serviced at the same time. – JohnRC Mar 30 '15 at 12:33
  • Also, I think maybe I have misunderstood the point of the question. The requirement is to SEND xml in a request and use the XML in the requested page, not the other way round, as I have done. – JohnRC Mar 30 '15 at 12:35
  • exactly, i want to send(post.asp) a XML to another site(receive.asp). I tried it on another server... there it works. – NyBu Mar 30 '15 at 12:42
  • No it doasn't but it doasn't load this long anymore. – NyBu Mar 30 '15 at 13:33
0

OK, here is my alternative answer, this time the page that you request sends an XML string to another page, which loads it into an XML document and extracts data from the XML.

[EDIT] The technique you have used to send XML in your original post works fine, see the final version (post3.asp /receive3.asp) shown below.

In post2.asp / receive2.asp I have encoded the XML as a form field named XML, then loaded that field into the XML object at the receiving end. This works fine for relatively small XML files.

In this example, you have to request the post2.asp page:

post2.asp

<%

    Response.write "Posting XML data to another page<br/>"

    '// URL to which to send XML data
    URL="http://localhost:8096/pages/test/receive2.asp"

    '// The XML string to be sent
    Dim Information: Information = "<Send><UserName>Colt</UserName><PassWord>Taylor</PassWord><Data>100</Data></Send>"

    dim XMLHttp : SET XMLHttp = Server.CreateObject("MSXML2.ServerXMLHttp")

    xmlhttp.Open "POST", url, false
    xmlhttp.setRequestHeader "Content-type", "application/x-www-form-urlencoded" 

    '// URLEncode the XML into a form data string and send it
    xmlhttp.send "xml=" & server.URLEncode( Information )

    '// Get the response that is send by receive2.asp
    dim sResponse: sResponse = xmlhttp.ResponseText

    '// Show the response
    Response.write "Response received from the called page:<hr/><span style='color:blue'>" & sResponse & "</span><hr/>"

%>

Receive2.asp

<%

    '// Get the xml string from the request
    xml = Request.form("xml")

    '// Write the received XML as a displayable html string
    response.write "[" & replace(replace(xml, "<", "&lt;"), ">", "&gt;") & "]<br/>"

    '// Create the XML object to load the string
     Dim objXml
     Set objXml = Server.CreateObject("MSXML2.DOMDOCUMENT.3.0")

     '// Try to load the xml string
    if objXML.LoadXML(xml) THEN

        '// Get the data from the xml
        strQuery = "//UserName"
        Set oNode = objXML.selectSingleNode(strQuery)
        strUserName = oNode.Text

        '// Success message
        response.write "success - user = " & strUserName & "<br/>"

    ELSE

        '// Failed message
        Response.Write "Failed to load XML file, reason: " & objXML.parseError.reason & "<br/>"

    END IF
%>

This is the result you get when opening the post2.asp page

Posting XML data to another page
Response received from the called page:
_______________________________________________________
[<Send><UserName>Colt</UserName><PassWord>Taylor</PassWord><Data>100</Data></Send>]
success - user = Colt
_______________________________________________________

Here is the final version, this time using the XML content type. You have to open the page post3.asp:

Post3.asp

<%

    '// URL to which to send XML data
    URL="http://localhost:8096/pages/test/receive3.asp"

    Response.Write "Sending XML data to " & URL & "<br/>"

    information = "<Send><UserName>Colt</UserName><PassWord>Taylor</PassWord><Data>100</Data></Send>"
    Set xmlhttp = server.Createobject("MSXML2.ServerXMLHTTP")
    xmlhttp.Open "POST", url, false
    xmlhttp.setRequestHeader "Content-Type", "text/xml" 
    xmlhttp.send information

    '// Report the response from the called page
    response.write "Response received:<hr/><span style='color:blue'>" & xmlhttp.ResponseText & "</span><hr/>"

%>

Receive3.asp

<%

    Dim objXmlRequest
    Set objXmlRequest = Server.CreateObject("MSXML2.DOMDOCUMENT.3.0")

    IF objXmlRequest.Load (Request) THEN

      'GET THE REQUEST FROM CLIENT
      strQuery = "//UserName"
      Set oNode = objXmlRequest.selectSingleNode(strQuery)
      strActionName = oNode.Text
      response.write "success! user name is " & strActionName  

    ELSE

        Response.Write "Failed to load XML file, reason: " & objXmlRequest.parseError.reason 

    END IF
%>

and the result is:

Sending XML data to http://localhost:8096/pages/test/receive3.asp

Response received:
success! user name is Colt
JohnRC
  • 1,251
  • 1
  • 11
  • 12
  • I guess you misunderstood me :/ I'm realy sorry if it wasn't clear what i want. The code you posted works fine. If i open the Post3.asp it shows me the name but i want to see the name if i open the Receive3.asp. If i open the Receive3.asp it says: Failed to load XML file, reason: XML document must have a top level element. – NyBu Mar 30 '15 at 14:04
  • @NyBu - Sorry I did not respond to your last comment - did you find a solution to this? – JohnRC May 12 '15 at 15:31
  • @NyBu - Re-reading your original posting, I have another question - where is the XML file located that you are trying to receive? You have to send the XML in the page request when you call Receive3.asp. So simply opening the page won't work, as there would be no XML supplied in the request. – JohnRC May 14 '15 at 10:54