3

I need to setup a web page that listens for XML document via an HTTP POST. I don't need to POST out, I need to receive that POST. What object does this? Should I use an HTTP handler, web service, webRequest, Stream or something else? I need to use a IIS Server and prefer C#.

I've Tried...

  1. I dont think I can use WebRequest since I'm not sending a request, just waiting for them.

  2. "HttpRequest.InputStream" but I'm not sure how to use it or where to put it. Do i need to use it with a web service or a asp.net application? I put it in 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"

If anyone has any helpful code or links that would be great!

EDIT: I am trying to receive notifications from another program.

Jeff LaFay
  • 12,882
  • 13
  • 71
  • 101
nubme
  • 2,857
  • 8
  • 29
  • 25

2 Answers2

13

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.

Here's an example of how you could write a generic handler (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;
        }
    }
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • I dont need to return anything, I'm just receiving notifications from another program. I tried Request.InputStream, and put a breakpoints in the code. It doesn't seem to catch any notifications. My code is very similar to the link above for try #2. I put it in Page_load. Does a POST Request trigger Page_load each time? – nubme Apr 06 '12 at 20:44
  • 1
    HTTP is a request/response based protocol. So you must always return something. You could return a 200 status code with an empty body indicating to the client the success of the operation. – Darin Dimitrov Apr 06 '12 at 20:47
  • I see, I'll try out the code and let you know. Thanks for the help! =] – nubme Apr 06 '12 at 20:52
  • So i got your code to work i think... I'm using wfetch to sent GET & POST requests. The code is able to see the GET request, but nothing happens when i send POST. Any idea? Thanks! – nubme Apr 06 '12 at 23:05
  • nm you gotta ping the exact page, can't just do localhost – nubme Apr 06 '12 at 23:31
0

I'm not sure where to call or use the handler. This is what i have so far...

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;
        }
      }
   }
}
nubme
  • 2,857
  • 8
  • 29
  • 25