繁体   English   中英

检索 HTTP 包含 XML 的 POST 请求

[英]Retrieve HTTP POST request containing XML

我需要设置一个 web 页面,通过 HTTP POST 侦听 XML 文档。 我不需要发布,我需要收到那个帖子。 object 是做什么的? 我应该使用 HTTP 处理程序、web 服务、webRequest、Stream 还是其他? 我需要使用 IIS 服务器并且更喜欢 C#。

我试过了...

  1. 我不认为我可以使用 WebRequest,因为我没有发送请求,只是在等待它们。

  2. “HttpRequest.InputStream”,但我不确定如何使用它或将它放在哪里。 我需要将它与 web 服务或 asp.net 应用程序一起使用吗? 我放在http://forums.asp.net/t/1371873.aspx/1

  3. 我尝试了一个简单的 web 服务http://msdn.microsoft.com/en-us/library/bb412178.aspx - 但是当我尝试访问“http://localhost:8000/EchoWithGet?s=Hello, world ,, 我收到“无法找到网页错误”

如果有人有任何有用的代码或链接,那就太好了!

编辑:我正在尝试接收来自另一个程序的通知。

您可以编写一个 ASP.NET 应用程序,您将在 IIS 中托管,您可以在其中拥有一个 .ASPX 页面或一个通用的.ASHX 处理程序(取决于您希望如何格式化结果 - 您想要返回 HTML 还是其他类型) 然后读取Request.InputStream ,它将包含来自客户端的请求的主体。

以下是如何编写通用处理程序 ( MyHandler.ashx ) 的示例:

public class MyHandler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        var stream = context.Request.InputStream;
        byte[] buffer = new byte[stream.Length];
        stream.Read(buffer, 0, buffer.Length);
        string xml = Encoding.UTF8.GetString(buffer);

        ... do something with the XML

        // We only set the HTTP status code to 202 indicating to the
        // client that the request has been accepted for processing
        // but we leave an empty response body
        context.Response.StatusCode = 202;
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

我不确定在哪里调用或使用处理程序。 这是我到目前为止...

默认.aspx

<%@Page Inherits="WebApplication1._Default"%>
<%@OutputCache Duration="10" Location="Server" varybyparam="none"%>
<script language="C#" runat="server">
  void Page_Init(object sender, EventArgs args) {

  }     
}
</script>
<html>
  <body>
  </body>
</html>

默认.aspx.cs

namespace WebApplication1  
{
  public partial class _Default : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {
        HttpContext contex = Context;
        MyHandler temp = new MyHandler();
        temp.ProcessRequest(context);
    }
  }

    public class MyHandler : IHttpHandler
    {
       public void ProcessRequest(HttpContext context)
       {
         var stream = context.Request.InputStream;
         byte[] buffer = new byte[stream.Length];
         stream.Read(buffer, 0, buffer.Length);
         string xml = Encoding.UTF8.GetString(buffer);

         ... do something with the XML

        // We only set the HTTP status code to 202 indicating to the
        // client that the request has been accepted for processing
        // but we leave an empty response body
        context.Response.StatusCode = 202;
       }

      public bool IsReusable
      {
        get
        {
          return false;
        }
      }
   }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM