简体   繁体   English

检索 HTTP 包含 XML 的 POST 请求

[英]Retrieve HTTP POST request containing XML

I need to setup a web page that listens for XML document via an HTTP POST.我需要设置一个 web 页面,通过 HTTP POST 侦听 XML 文档。 I don't need to POST out, I need to receive that POST.我不需要发布,我需要收到那个帖子。 What object does this? object 是做什么的? Should I use an HTTP handler, web service, webRequest, Stream or something else?我应该使用 HTTP 处理程序、web 服务、webRequest、Stream 还是其他? I need to use a IIS Server and prefer C#.我需要使用 IIS 服务器并且更喜欢 C#。

I've Tried...我试过了...

  1. I dont think I can use WebRequest since I'm not sending a request, just waiting for them.我不认为我可以使用 WebRequest,因为我没有发送请求,只是在等待它们。

  2. "HttpRequest.InputStream" but I'm not sure how to use it or where to put it. “HttpRequest.InputStream”,但我不确定如何使用它或将它放在哪里。 Do i need to use it with a web service or a asp.net application?我需要将它与 web 服务或 asp.net 应用程序一起使用吗? I put it in http://forums.asp.net/t/1371873.aspx/1我放在http://forums.asp.net/t/1371873.aspx/1

  3. I've tried a simple web service http://msdn.microsoft.com/en-us/library/bb412178.aspx - But when i try to visit "http://localhost:8000/EchoWithGet?s=Hello, world,", i get a "webpage cannot be found error"我尝试了一个简单的 web 服务http://msdn.microsoft.com/en-us/library/bb412178.aspx - 但是当我尝试访问“http://localhost:8000/EchoWithGet?s=Hello, world ,, 我收到“无法找到网页错误”

If anyone has any helpful code or links that would be great!如果有人有任何有用的代码或链接,那就太好了!

EDIT: I am trying to receive notifications from another program.编辑:我正在尝试接收来自另一个程序的通知。

You could write an ASP.NET application that you will host in IIS in which you could either have an.ASPX page or a generic .ASHX handler (depending on how you want the result to be formatted - do you want to return HTML or some other type) and then read the Request.InputStream which will contain the body of the request that comes from the client.您可以编写一个 ASP.NET 应用程序,您将在 IIS 中托管,您可以在其中拥有一个 .ASPX 页面或一个通用的.ASHX 处理程序(取决于您希望如何格式化结果 - 您想要返回 HTML 还是其他类型) 然后读取Request.InputStream ,它将包含来自客户端的请求的主体。

Here's an example of how you could write a generic handler ( MyHandler.ashx ):以下是如何编写通用处理程序 ( 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;
        }
    }
}

I'm not sure where to call or use the handler.我不确定在哪里调用或使用处理程序。 This is what i have so far...这是我到目前为止...

Default.aspx默认.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默认.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