반응형

 


// http://localhost:8080/getPersonHandler/333


using System;
using System.IO;
using System.Net.Http;
using System.Net;
using System.Threading.Tasks;
using System.Windows;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Linq;

namespace RestApiServer
{
    internal class Mapping : Attribute
    {
        public string Map;

        public Mapping(string s)
        {
            Map = s;
        }
    }

    public partial class MainWindow : Window
    {
        private HttpListener _httpListener;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void btnServiceStart_Click(object sender, RoutedEventArgs e)
        {
            InitServer();
        }

        private void InitServer()
        {
            if (_httpListener == null)
            {
                _httpListener = new HttpListener();
                _httpListener.Prefixes.Add(string.Format("http://+:9000/"));
                StartServer();
            }
        }

        [Mapping("Person")]
        public void GetPersonHandler(int id)
        {
            this.Dispatcher.Invoke(new Action(() =>
            {
                txtLog.Text = $"GetPersonHandler : {id}";
            }));
        }

        private void StartServer()
        {
            if (!_httpListener.IsListening)
            {
                _httpListener.Start();
                txtLog.Text = "Server is started";

                Task.Factory.StartNew(() =>
                {
                    while (_httpListener != null)
                    {
                        HttpListenerContext context = this._httpListener.GetContext();

                        string methodName = context.Request.Url.Segments[1].Replace("/", "");
                        string[] strParams = context.Request.Url
                                                .Segments
                                                .Skip(2)
                                                .Select(s => s.Replace("/", ""))
                                                .ToArray();

                        var method = this.GetType()
                                            .GetMethods()
                                            .Where(mi => mi.GetCustomAttributes(true).Any(attr => attr is Mapping && ((Mapping)attr).Map == methodName))
                                            .First();

                        object[] @params = method.GetParameters()
                                            .Select((p, i) => Convert.ChangeType(strParams[i], p.ParameterType))
                                            .ToArray();

                        object ret = method.Invoke(this, @params);
                        string retstr = JsonConvert.SerializeObject(ret);

                        context.Response.Close();
                    }
                });
            }
        }
    }
}

 

 

 

참고 : https://stackoverflow.com/questions/10017564/url-mapping-with-c-sharp-httplistener

 

URL mapping with C# HttpListener

In the code below I am waiting for any call to the 8080 port. public static void Main() { HttpListener listener = new HttpListener(); listener.Prefixes.Add("http://*:8080/"); ...

stackoverflow.com

 

 

반응형

+ Recent posts