使用ASP.NET通过POST接收(和发送)XML
发布时间:2020-12-16 00:14:32 所属栏目:asp.Net 来源:网络整理
导读:我必须设置一个XML“Web服务”,它接收一个POST,其中’Content-type标头将指定“text / xml”. 将XML导入XDocument以便通过VB.NET的轴查询进行访问的最简单方法是什么? 我不相信Web服务可以保证遵循任何协议(例如SOAP等);只是针对各种请求的特定标签和子标签
我必须设置一个XML“Web服务”,它接收一个POST,其中’Content-type标头将指定“text / xml”.
将XML导入XDocument以便通过VB.NET的轴查询进行访问的最简单方法是什么? 我不相信Web服务可以保证遵循任何协议(例如SOAP等);只是针对各种请求的特定标签和子标签,它将使用基本身份验证,因此我将不得不处理标头. (如果重要: 解决方法
鉴于Steven的警告,答案可能是先用
Tom Holland’s test手动解析Request.InputStream,然后在Page_Load事件中解析XDocument.Load.
在我提出这个问题之前就已经开始了Google搜索,但只有在检查了this之后才进行了检查,这也表明我已经走上了正确的道路. 此外,我还要问一下我的观点暗示的问题,即响应必须是XML,关于什么是最好的方法,但我找到了答案here. 总之,最终的代码是: Protected Sub Page_Load(ByVal sender As Object,ByVal e As System.EventArgs) Handles Me.Load If Request.ContentType <> "text/xml" Then _ Throw New HttpException(500,"Unexpected Content-Type") Dim id = CheckBasicAuthentication Dim textReader = New IO.StreamReader(Request.InputStream) CheckXmlValidity(textReader) ' Reset the stream & reader Request.InputStream.Seek(0,IO.SeekOrigin.Begin) textReader.DiscardBufferedData() Dim xmlIn = XDocument.Load(textReader) ' process XML in xmlIn Dim xmlOut = <?xml version="1.0" encoding="UTF-8" ?> <someresult> <header> <id><%= id.ToString() %></id> <datestamp>To be inserted</datestamp> </header> <result/> </someresult> ' Further generation of XML for output xmlOut.<someresult>.<header>.<datestamp>.Value = Date.UtcNow.ToString(xmlDateFormat) xmlText.Text = xmlOut.ToString End Sub Private Function CheckBasicAuthentication() As Integer Dim httpAuthorisation = Request.Headers("Authorization") If Left(httpAuthorisation,6).ToUpperInvariant <> "BASIC " Then _ Throw New HttpException(401,"Basic Authentication Required") Dim authorization = Convert.FromBase64String(Mid(httpAuthorisation,7)) Dim credentials = Text.Encoding.UTF8.GetString(authorization).Split(":"c) Dim username = credentials(0) Dim password = credentials(1) Return ConfirmValidUser(username,password) End Function Private Shared Sub CheckXmlValidity(ByVal textReader As System.IO.StreamReader) Try ' Check for "interesting" xml documents. Dim settings = New System.Xml.XmlReaderSettings() settings.XmlResolver = Nothing settings.MaxCharactersInDocument = 655360 ' Successfully parse the file,otherwise an XmlException is to be thrown. ' Dim reader = System.Xml.XmlReader.Create(textReader,settings) Try While reader.Read() 'Just checking. End While Finally reader.Close() End Try Catch ex As Exception Throw New HttpException(500,"Invalid Xml data",ex) End Try End Sub 和ASP.NET webpage.aspx是: <%@ Page Language="VB" AutoEventWireup="false" CodeFile="webpage.aspx.vb" Inherits="WebPage" ContentType="text/xml" %> <asp:Literal ID="xmlText" runat="server" Mode="PassThrough"></asp:Literal> NB抛出HTTPException不是有害场景的有效最终解决方案. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – 由TaskScheduler和SynchronizationContext混淆同
- asp.net – 界面的好处是什么意味着某种实现?
- asp.net-mvc – 如何从VOID方法重定向到MVC3中的另一个动作
- asp-classic – 动态增加数组大小
- WPF 增加合计一栏
- asp.net中的基本形式认证安全性如何?
- 实时监控ASP.NET Web应用程序的工具和方法?
- asp.net – CORS错误 – 请求的资源上没有“Access-Control
- asp.net-mvc – 资源解释为Document但使用MIME类型applicat
- asp.net – 无法加载文件或程序集System.Web.Http,版本= 5.
推荐文章
站长推荐
- asp.net中MVC借助Iframe实现无刷新上传文件实例
- asp.net – 没有角色的声明?
- asp.net – 如何让Html Link(锚点)回发像LinkBut
- asp.net-mvc – 关于使用重写的AuthorizeAttribu
- 从代码隐藏调用ASP.NET Web API
- asp.net – 使用母版页在Aspx页面中引用CSS工作表
- ASP.NET MVC对DateTime有什么格式,以便模型绑定能
- asp.net-mvc – MVC 4是否有内置的站点地图解决方
- ASP.NET -- WebForm -- ViewState
- asp.net-mvc – 使用ToString()和@ Html.Display
热点阅读