【问题标题】:Classic ASP post and receive XML经典 ASP 发送和接收 XML
【发布时间】:2015-03-30 08:35:08
【问题描述】:

嘿,我已经尝试了几个小时通过经典的 ASP 页面接收 XML,但它就是无法正常工作。

这是我的代码:

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

出现这个错误:

加载 XML 文件失败,原因:XML 文档必须有顶级元素。

我只是不明白。另外我想知道是否可以保存加载的 XML?

【问题讨论】:

标签: xml vbscript asp-classic


【解决方案1】:

试试这个

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

%>

这是打开页面“receive.asp”时的结果(在我的例子中是 http://localhost:8096/pages/test/receive.asp)

http://localhost:8096/pages/test/post.asp
DOMDocument
success - user = Colt

【讨论】:

  • 试过了...不起作用:/receive.asp 需要几分钟才能加载,加载后它只显示 post.asp 的 URL...
  • 在我的服务器上几乎是即时的。您使用的是完整版的 IIS 吗?我认为windows自带的IIS版本(不是服务器版本)不允许同时服务多个请求。
  • 另外,我想我可能误解了问题的重点。要求是在请求中发送 xml 并在请求的页面中使用 XML,而不是像我所做的那样。
  • 没错,我想发送(post.asp)一个 XML 到另一个站点(receive.asp)。我在另一台服务器上尝试过......它可以工作。
  • 不,它不会,但它不会再加载这么长时间了。
【解决方案2】:

好的,这是我的替代答案,这次您请求的页面将 XML 字符串发送到另一个页面,该页面将其加载到 XML 文档中并从 XML 中提取数据。

[编辑]您在原始帖子中用于发送 XML 的技术工作正常,请参阅下面显示的最终版本 (post3.asp /receive3.asp)。

在 post2.asp / receive2.asp 中,我将 XML 编码为名为 XML 的表单字段,然后将该字段加载到接收端的 XML 对象中。这适用于相对较小的 XML 文件。

在这个例子中,你必须请求 post2.asp 页面:

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
%>

这是你打开post2.asp页面时得到的结果

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
_______________________________________________________

这是最终版本,这次使用 XML 内容类型。你要打开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
%>

结果是:

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

Response received:
success! user name is Colt

【讨论】:

  • 我猜你误会了我:/如果不清楚我想要什么,我真的很抱歉。您发布的代码工作正常。如果我打开 Post3.asp,它会显示名称,但如果我打开 Receive3.asp,我想查看名称。如果我打开 Receive3.asp 它说:加载 XML 文件失败,原因:XML 文档必须有顶级元素。
  • @NyBu - 抱歉,我没有回复您的上一条评论 - 您找到解决方案了吗?
  • @NyBu - 重新阅读您的原始帖子,我还有一个问题 - 您尝试接收的 XML 文件位于何处?当您调用 Receive3.asp 时,您必须在页面请求中发送 XML。所以简单地打开页面是行不通的,因为请求中没有提供 XML。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-03
相关资源
最近更新 更多