检索包含XML的HTTP POST请求

本文关键字:POST 请求 HTTP 包含 XML 检索 | 更新日期: 2023-09-27 18:30:11

我需要设置一个通过HTTPPOST侦听XML文档的网页。我不需要POST输出,我需要接收POST。这是什么物体?我应该使用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世界",我得到一个"网页找不到错误"

如果有人有任何有用的代码或链接,那将是伟大的!

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

检索包含XML的HTTP POST请求

您可以编写一个ASP.NET应用程序,该应用程序将托管在IIS中,在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;
        }
    }
}

我不确定在哪里调用或使用处理程序。这就是我目前所拥有的。。。

Default.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>

Default.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;
        }
      }
   }
}