通过POST接收(和发送)XML与ASP.NETXML、POST、NET、ASP

2023-09-03 00:16:30 作者:空港里的旧少年。

我要成立一个XMLWeb服务接收一个帖子里的Content-type头会指定为text / xml的。

I have to set up an XML "web service" that receives a POST where the 'Content-type header will specify "text/xml".'

什么是VB.NET的轴查询来获取XML到的XDocument 访问最简单的方法是什么?

What is the simplest way to get the XML into an XDocument for access by VB.NET's axis queries?

我不相信Web服务是保证遵循任何协议(如SOAP等);只是特定的标签和子标签的各种要求,并且将使用基本身份验证,所以我必须处理的头。

I don't believe the web service is guaranteed to follow any protocol (e.g. SOAP, etc); just specific tags and sub-tags for various requests, and it will use Basic Authentication, so I will have to process the headers.

(如果它的事项:  *真人版将使用HTTPS和  *的响应也将是XML。)

(If it matters: * the live version will use HTTPS, and * the response will also be XML.)

推荐答案

由于史蒂芬的警告,答案可能是解析 Request.InputStream 手动汤姆·霍兰德的测试第一,其次是 XDocument.Load 的Page_Load 事件

Given Steven's warning, the answer may be to parse Request.InputStream manually with Tom Holland's test first, followed by XDocument.Load in the Page_Load event.

有一个谷歌搜索发起之前,我问的问题,但毕竟只是检查,发现这个,还建议我在正确的轨道上。

A Google search initiated before I asked the question, but only checked after, found this, also suggesting I'm on the right track.

此外,我要问的反应必须是XML过,至于究竟是什么,最好的方式通过我的观点所隐含的问题,但我已经找到答案的这里。

Also I was going to ask the question implied by my point that the response had to be XML too, as to what is the best way for that, but I've found an answer here.

总之,最后的code是:

In summary, the final code is:

 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是:

and the ASP.NET webpage.aspx is:

<%@ 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 是不是不需要的情况下有效的最终解决方案。

NB Throwing HTTPException is not a valid final solution for unwanted scenarios.