我花了一整天的时间在使用ASP.NET。我创建了一个.aspx页面,它将XML发送到其他.aspx页面,该页面读取以前的XML并向第一页发送响应。
第一页似乎有效,但第二页失败。我怀疑它是环境中的东西,而不是ASP代码,但我无法理解这一点。
让我们揭露环境: - IIS 6.0 - ASP.NET版本2.0.50727 - Windows Server 2003 R2 SP2标准版。版
(已在IIS 7.5,ASP.NET 2和4,Windows Server 2008R2中测试过,结果相同)
现在,让我们公开网页代码。
SimplePoster.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>
Receiver.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)
%>
现在,让我们用IE打开http://myserver/SimplePoster.aspx
。输出是:
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
那么,根本没有输入流?为什么呢?
答案 0 :(得分:1)
最后我成功了。谷歌搜索和duckduckgoed,睡得更少。安装小提琴并使用它让我看到SimplePoster2根本没有发送任何内容,并注意到带有StreamReader的Receiver2实际上正在接收并响应由Fiddler直接发送的XML。
Sooooo花了很多时间在雨中流下眼泪......&lt; = ;-)极客眨眼!
我与你分享我的课程。
最终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对象,将其内容作为字符串发送。
DO:[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]
只有2个方法:Load读取带有XML文本的文件,LoadXML输入XML字符串。
请勿尝试直接在MSXML中输入Request。使用StreamReader读取输入,将其转换为字符串,然后将其输入MSXML。现在,您可以根据需要使用收到的XML。