'C# How to RECEIVE data from post [closed]

I have a C# console application that communicates with the API of the SLACK chat client. I am able to post data to SLACK without a problem. Now I would like to do the reverse.

In SLACK I can specify a URL to POST to. I would like to specify a URL, and configure my C# project to receive the POST. HELP!?



Solution 1:[1]

You could use HttpListener.

Simple example base on this article by Harold Hunt:

public delegate byte[] ProcessDataDelegate(string data);

public class SimpleServer
{
    private const int HandlerThread = 2;
    private readonly ProcessDataDelegate handler;
    private readonly HttpListener listener;

    public SimpleServer(HttpListener listener, string url, ProcessDataDelegate handler)
    {
        this.listener = listener;
        this.handler = handler;
        listener.Prefixes.Add(url);
    }

    public void Start()
    {
        if (listener.IsListening)
            return;

        listener.Start();

        for (int i = 0; i < HandlerThread; i++)
        {
            listener.GetContextAsync().ContinueWith(ProcessRequestHandler);
        }
    }

    public void Stop()
    {
        if(listener.IsListening)
            listener.Stop();
    }

    private void ProcessRequestHandler(Task<HttpListenerContext> result)
    {
        var context = result.Result;

        if (!listener.IsListening)
            return;

        // Start new listener which replace this
        listener.GetContextAsync().ContinueWith(ProcessRequestHandler);

        // Read request
        string request = new StreamReader(context.Request.InputStream).ReadToEnd();

        // Prepare response
        var responseBytes = handler.Invoke(request);
        context.Response.ContentLength64 = responseBytes.Length;

        var output = context.Response.OutputStream;
        output.WriteAsync(responseBytes, 0, responseBytes.Length);
        output.Close();
    }
}

And after them you can simple http server from your console app:

    static void Main(string[] args)
    {
        StartServer();
        Console.ReadKey();
    }

    public static void StartServer()
    {
        var httpListener = new HttpListener();
        var simpleServer = new SimpleServer(httpListener, "http://127.0.0.1:1234/test/", ProcessYourResponse);
        simpleServer.Start();
    }

    public static byte[] ProcessYourResponse(string test)
    {
        Console.WriteLine(test);
        return new byte[0]; // TODO when you want return some response
    }

Solution 2:[2]

There are lots of ways to create a web service in C#. WCF, WebAPI, ASMX, low-level sockets and all kinds of open source frameworks as well.

It doesn't fit into a console app but the easiest of these, in my opinion, is WebAPI which is an ASP.NET web application.

If you must use a console app, I would look at Katana.

Example of Katana in Console app

Solution 3:[3]

GET is the HTTP method to get data from servers. To point to different data sources, you have to make the request to different URIs, For example, if you want to get some messages from a conversation given a searching query, you have to make a GET HTTP request to https://slack.com/api/search.messages passing the required parameters.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mateen Ulhaq
Solution 2 chief7
Solution 3 Andy