我整天都在为 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
那么,根本没有输入流吗?为什么?