Search code examples
c#asp.net.netweb-applicationshttpwebrequest

Receive Web Request and Process


I am new to web application domain and I want to know how to deploy a web page and expose that for a particular party. I've an application that receives SMS from another application. I need to provide a web page for the other application in order to send messages to my application.

I basically want to expose MessageReceive.aspx page and receive requests like below. I know how to process query strings but not sure the best way to expose a page to a third party application on internet?

http://www.Mysite.com.au/MessageReceive.aspx?ORIGINATOR=61412345678&RECIPIENT=1987654&MESSAGE_TEXT=Hello%20There!

Do I need to deploy "MessageReceive.aspx" page as a web application on IIS? If so could you please point me an example?

How about using HttpListener class in a windows service? Is that capable of doing this?

Thanks!


Solution

  • The HttpListener class is indeed capable of hosting an endpoint like that inside of any kind of application (i.e. Windows Desktop app, Windows Service, Console app, etc.) Using HttpListener in a serial manner where a single request at a time can be processed is quite straightforward, however using it to provide any amount of concurrency can quickly get quite complex.

    If you do wish to host a serial endpoint in a windows service, HttpListener is definitely the quickest approach. All it really takes is something like this:

    // To start:
    var listener = new HttpListener("http://www.mysite.com.au/message/");
    listener.Start();
    
    // To stop:
    listener.Stop();
    listener.Close();
    
    // In background thread:
    while (listener.IsListening)
    {
        var context = listener.GetContext(); // Will block until a request is received
        // TOD: Use the context variable (HttpListenerContext type) to get query string parameters and/or the request stream, process data, and configure a response
    }
    

    A simple program like that will only process one request at a time, however the HttpListener can queue up quite a number of requests at a time. If you don't intend to handle high load with your service, it should be sufficient. If you need to handle high load and need concurrent request processing, you'll need to use the BeginGetContext/EndGetContext methods and asynchronous programming. The burden is on you the developer to deal with all the complexities of concurrent programming, throttling, safe and secure shutdown, etc. (It should be noted that calls to EndGetContext tend to throw if called while the HttpListener is being shut down, which is possible since the ThreadPool is responsible for executing the callback handler of asynchronous calls.)