0

我整天都在为 ASP.NET 苦苦挣扎。我制作了一个 .aspx 页面,该页面将 XML 发送到其他 .aspx 页面,该页面读取先前的 XML 并向第一页发送响应。

第一页似乎工作,但第二个失败。我怀疑这是环境中的东西,而不是 ASP 代码,但我不明白这一点。

让我们公开环境: - IIS 6.0 - ASP.NET 版本 2.0.50727 - Windows Server 2003 R2 SP2 Std。版

(这已经在 IIS 7.5、ASP.NET 2 和 4、Windows Server 2008R2 中进行了测试,结果相同)

现在,让我们公开网页代码。

简单海报.aspx

<%@Page AspCompat=true Language = VB%>
<HTML>
<HEAD>
</HEAD>
<%
'Put together some XML to post off
Dim xmlString = "<?xml version=""1.0""?>" & vbcrlf
xmlString = xmlString & "<Req1>" & vbcrlf
xmlString = xmlString & " <Name>Jenny</Name>" & vbcrlf
xmlString = xmlString & "</Req1>"

'Load the XML into an XMLDOM object
Dim SendDoc = server.createobject("Microsoft.XMLDOM")
SendDoc.ValidateOnParse= True
SendDoc.LoadXML(xmlString)

'Set the URL of the receiver
Dim sURL = "http://myserver/Receiver.aspx"

'Call the XML Send function (defined below)
Dim poster = Server.CreateObject("MSXML2.ServerXMLHTTP")
poster.open("POST", sURL, false)
poster.setRequestHeader("CONTENT_TYPE", "text/xml")
poster.send(SendDoc)
Dim NewDoc = server.createobject("Microsoft.XMLDOM")
newDoc.ValidateOnParse= True
newDoc.LoadXML(poster.responseTEXT)

'We receive back another XML DOM object!

'Tell the user what happened
response.Write ("<b>XML DOC posted off:</b><br>")
response.write (SendDoc.XML & "<br>")
response.write ("<b>Target URL:</b> " & sURL & "<br>")
response.write ("<b>XML DOC Received back: </b><br>")
response.write (NewDoc.Xml)
%>
</HTML>

接收器.aspx

<%@Page AspCompat=true Language = VB%>
<%
'Create an XML DOM Object to receive the request
'dim docReceived = CreateObject("Microsoft.XMLDOM") 'docReceived.load(request) Valid only in IIS 5.0?
dim docReceived = CreateObject("Msxml2.DOMDocument.6.0")
docReceived.async = False
docReceived.load(Request)

'Create a piece of XML to send back
Dim listItem = docReceived.selectnodes("Req1")

Dim strResponse = "<?xml version=""1.0""?>" & vbcrlf
strResponse = strResponse & "<Person>" & vbcrlf

'For the purposes of this example we modify
'the response based on the request
Dim node
Dim name
for each node in listItem
name = node.selectsinglenode("Name").firstchild.nodevalue
strResponse = strResponse & " <Name>Thanks " & name & "</Name>" & vbcrlf
next

strResponse = strResponse & "</Person>"

'Send the response back
response.write(strResponse)
%>

现在,让我们http://myserver/SimplePoster.aspx用 IE 打开。输出是:

XML DOC posted off:
Jenny 
Target URL: http://localhost/Test1/Receiver.aspx
XML DOC Received back:

请注意,响应是空的......现在让我们打开http://localhost/Receiver.aspx

Server Error in '/' Application.
--------------------------------------------------------------------------------

Value does not fall within the expected range. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.ArgumentException: Value does not fall within the expected range.

Source Error: 


Line 5:  dim docReceived = CreateObject("Msxml2.DOMDocument.6.0")
Line 6:  docReceived.async = False
Line 7:  docReceived.load(Request)
Line 8:  
Line 9:  'Create a piece of XML to send back


Source File: C:\Inetpub\TestAxXMLInbox\Receiver.aspx    Line: 7 

Stack Trace: 


[ArgumentException: Value does not fall within the expected range.]
   Microsoft.VisualBasic.CompilerServices.LateBinding.InternalLateCall(Object o, Type objType, String name, Object[] args, String[] paramnames, Boolean[] CopyBack, Boolean IgnoreReturn) +776
   Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateCall(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack, Boolean IgnoreReturn) +367336
   ASP.test1_receiver_aspx.__Render__control1(HtmlTextWriter __w, Control parameterContainer) in C:\Inetpub\TestAxXMLInbox\Test1\Receiver.aspx:7
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +98
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +20
   System.Web.UI.Page.Render(HtmlTextWriter writer) +26
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +25
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +121
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +22
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2558




--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.1433; ASP.NET Version:2.0.50727.1433 

现在,百万美元的问题……是什么引起了这个问题? *如何在 ASP 页面中接收 XML(或文本)?*


我仍在为此苦苦挣扎。

我尝试了 XMLDocument.load/XMLDocument.loadXML (Request/Request.InputStream) 的组合。没运气。

为了测试这一点,如果我将 Receiver.aspx 设置为

<%@Page AspCompat=true Language=VB ValidateRequest=false%>
<%
    Dim reqLen As Integer = Request.TotalBytes

'Create a piece of XML to send back

    Dim strResponse As String = "<?xml version=""1.0""?><equis><comment> " & reqLen & " bytes</comment><footer>End of document</footer></equis>"

'Send the response back
response.write(strResponse)
%>

结果总是:

XML DOC posted off:
 Jenny  
Target URL: http://localhost:81/Receiver.aspx
XML DOC Received back: 
  0 bytesEnd of document

那么,根本没有输入流吗?为什么?

4

1 回答 1

1

最后我成功了。谷歌搜索,duckduckgoed 很多,睡得少。安装 Fiddle 并使用它让我看到 SimplePoster2 根本没有发送任何内容,并注意到带有 StreamReader 的 Receiver2 确实在接收和响应直接从 Fiddler 组合和发送的 XML。

这么多小时像雨中的眼泪一样度过和失去...... <= ;-) 极客眨眼!

我与你分享我的经验教训。

最终的 SimplePoster2.aspx:

<%@Page AspCompat=true Language = VB%>

<HTML>
<HEAD>
</HEAD>
<%

    'Put together some XML to post off
    Dim xmlString As String = "<?xml version=""1.0""?>" & vbCrLf
    xmlString = xmlString & "<Req1>" & vbCrLf
    xmlString = xmlString & " <Name>Jenny</Name>" & vbCrLf
    xmlString = xmlString & "</Req1>"    

    'Load the XML into an XMLDOM object
    Dim msXMLDocu As Object = Server.CreateObject("Msxml2.DOMDocument.6.0")
    'msXMLDocu.ValidateOnParse = True
    msXMLDocu.async = False
    msXMLDocu.LoadXML(xmlString)

    'Set the URL of the receiver
    Dim sURL As String = "http://localhost:81/Receiver2.aspx"

    'Call the XML Send function (defined below)
    Dim xmlPoster As Object = Server.CreateObject("MSXML2.ServerXMLHTTP")
    xmlPoster.open("POST", sURL, False)
    xmlPoster.setRequestHeader("Content-Type", "text/xml")
    xmlPoster.send(msXMLDocu.xml)
    Dim NewXmlDoc = Server.CreateObject("Msxml2.DOMDocument.6.0")
    NewXmlDoc.async = False
    'NewXmlDoc.ValidateOnParse = True
    NewXmlDoc.LoadXML(xmlPoster.responseText)

    'We receive back another XML DOM object!

    'Tell the user what happened
    Response.Write("<b>XML DOC posted off:</b><br>")
    Response.Write(msXMLDocu.XML & "<br>")
    Response.Write("<b>Target URL:</b> " & sURL & "<br>")
    Response.Write("<b>XML DOC Received back: </b><br>")
    Response.Write(NewXmlDoc.Xml)

%>
</HTML>

不要:[MSXML2.ServerXMLHTTP].send([Msxml2.DOMDocument.6.0])

不要尝试发送 XML Doc 对象,将其内容作为字符串发送。

做:[MSXML2.ServerXMLHTTP].send([Msxml2.DOMDocument.6.0].xml)

简单的Receiver2.aspx:

<%@Page AspCompat=true Language=VB ValidateRequest=false%>
<%
    Dim strResponse As String
    Dim reqLen As Integer = Request.TotalBytes

    If (reqLen > 0) Then
        Request.InputStream.Position = 0
        Dim sReader As System.IO.StreamReader = New System.IO.StreamReader(Request.InputStream)
        Dim inputStr As String = sReader.ReadToEnd()

        Dim msXmlDocu As Object = CreateObject("Msxml2.DOMDocument.6.0")
        msXmlDocu.async = False
        Dim xmlLoaded As Boolean = msXmlDocu.loadXML(inputStr)
        If (xmlLoaded) Then
            Dim listItem As Object = msXmlDocu.selectnodes("Req1")

            strResponse = "<?xml version=""1.0""?>" & vbCrLf & "<doc><status>OK</status><length>" & reqLen & " bytes</length>"
            strResponse = strResponse & "<Person>" & vbCrLf

            'For the purposes of this example we modify
            'the response based on the request
            Dim node
            Dim name
            For Each node In listItem
                name = node.selectsinglenode("Name").firstchild.nodevalue
                strResponse = strResponse & " <Name>Thanks " & name & "</Name>" & vbCrLf
            Next

            strResponse = strResponse & "</Person></doc>"

            'strResponse = "<?xml version=""1.0""?><doc><status>OK</status><length>" & reqLen & " bytes</length><content>" & stri & " </content></doc>"
        Else
            'Create a piece of XML to send back
            strResponse = "<?xml version=""1.0""?><doc><status>Error</status><reason>Data posted, but could not load XML Document</reason></doc>"
        End If
    Else
        'Create a piece of XML to send back
        strResponse = "<?xml version=""1.0""?><doc><status>Error</status><reason>No data posted available</reason></doc>"
    End If


        'Send the response back
        Response.Write(strResponse)
%>

不要:[Msxml2.DOMDocument.6.0].load(Request)[Msxml2.DOMDocument.6.0].load(Request.InputStream)

做:

Request.InputStream.Position = 0
Dim sReader As System.IO.StreamReader = New System.IO.StreamReader(Request.InputStream)
Dim inputStr As String = sReader.ReadToEnd()
[Msxml2.DOMDocument.6.0].loadXML(inputStr)

[Msxml2.DOMDocument.6.0]只有两种方法:Load 读取一个包含 XML 文本的文件,LoadXML 输入一个 XML 字符串。

不要尝试直接在 MSXML 中输入 Request。使用 StreamReader 读取输入,将其转换为字符串,然后将其输入到 MSXML。现在您可以根据需要使用接收到的 XML。

于 2014-03-14T12:59:16.530 回答