我需要设置一个通过 HTTP POST 监听 XML 文档的网页。我不需要发布,我需要接收该 POST。这是什么对象?我应该使用 HTTP 处理程序、Web 服务、webRequest、Stream 还是其他什么?我需要使用 IIS 服务器并且更喜欢 C#。

我试过了...

  • 我不认为我可以使用 WebRequest,因为我没有发送请求,只是在等待它们。
  • "HttpRequest.InputStream"但我不确定如何使用它或将它放在哪里。我需要将它与 Web 服务或 asp.net 应用程序一起使用吗?我把它放进去
    http://forums.asp.net/t/1371873.aspx/1
  • 我尝试了一个简单的网络服务 http://msdn.microsoft.com/en-us/library/bb412178.aspx - 但是当我尝试访问“http://localhost:8000/EchoWithGet?s=Hello, world!”时,我收到一个“网页无法找到错误”

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

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

    最佳答案

    您可以编写一个将在 IIS 中托管的 ASP.NET 应用程序,您可以在其中拥有一个 .ASPX 页面或一个通用的 .ASHX handler(取决于您希望如何格式化结果 - 您想返回 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;
            }
        }
    }
    

    关于c# - 检索包含 XML 的 HTTP POST 请求,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10049003/

    10-12 01:35
    查看更多