public void Start() { listener = new HttpListener(); if (!HttpListener.IsSupported) { return; } listener.Prefixes.Add(ip); try { listener.Start(); Console.WriteLine("Server started successfully"); } catch (Exception e) { Console.WriteLine(e.Message); Console.ReadLine(); } while (listener.IsListening) { Console.WriteLine("Waiting for request.."); HttpListenerContext context = listener.GetContext(); Console.WriteLine("Request received"); Console.WriteLine(context.Request.RemoteEndPoint); // receive incoming request HttpListenerRequest request = context.Request; if (request.HttpMethod == "POST") { ShowRequestData(request); } if (!flag) { return; } //string responseString = @"<!DOCTYPE HTML> // <html><head></head><body> // <form method=""post"" action=""""> // <p><b>Name: </b><br> // <input type=""text"" name=""myname"" size=""40""></p> // <p><input type=""submit"" value=""send""></p> // </form></body></html>"; //HttpListenerResponse response = context.Response; //response.ContentType = "text/html; charset=UTF-8"; //byte[] buffer = Encoding.UTF8.GetBytes(responseString); //response.ContentLength64 = buffer.Length; HttpListenerResponse response = context.Response; TextReader tr = new StreamReader("data.json"); string msg = tr.ReadToEnd(); byte[] buffer = Encoding.UTF8.GetBytes(msg); response.ContentType = "application/json"; response.ContentLength64 = buffer.Length; using (Stream output = response.OutputStream) { output.Write(buffer, 0, buffer.Length); } } }
public void Start() { _http.Start(); Task.Run(() => { while (_http.IsListening) { try { var context = _http.GetContext(); var req = context.Request; var res = context.Response; Console.WriteLine($"[{DateTime.Now.ToLongTimeString()}] {req.HttpMethod}: {req.RawUrl} from {req.RemoteEndPoint.Address}"); if (req.HttpMethod == "GET") { switch (req.RawUrl) { case "/data": SendJson(res, JsonConvert.SerializeObject(new Animal() { Id = Guid.NewGuid(), Name = "Vaska", Type = "Cat", Age = 0.3f })); break; case "/all": SendJson(res, JsonConvert.SerializeObject(_db.GetAllAnimalsAsync().Result)); break; default: SendHtml(res, "Hello. Make GET /data to get json"); break; } } else if (req.HttpMethod == "POST") { var buffer = new byte[req.ContentLength64]; req.InputStream.ReadAsync(buffer, 0, buffer.Length); var incoming = _encoding.GetString(buffer); Console.WriteLine($"Received data: {incoming}"); switch (req.RawUrl) { case "/me": { var token = JsonConvert.DeserializeObject <int>(incoming); var person = _cache.GetPersonAsync(token).Result; if (person == null) { SendHtml(res, "We don't know you"); } else { var animals = _db.GetTakenAnimalsByAsync(person); SendJson(res, JsonConvert.SerializeObject(animals.Result)); } } break; case "/mebyemail": { var email = JsonConvert.DeserializeObject <string>(incoming); var token = email.GetHashCode(); var person = _cache.GetPersonAsync(token).Result; if (person == null) { SendHtml(res, "We don't know you"); } else { var animals = _db.GetTakenAnimalsByAsync(person); SendJson(res, JsonConvert.SerializeObject(animals.Result)); } } break; case "/add": { var animal = JsonConvert.DeserializeObject <Animal>(incoming); if (animal == null) { SendHtml(res, "Error: couldn't deserialize given Animal"); } else { _db.AddAnimalAsync(animal); SendHtml(res, "Animal added successfully"); } } break; case "/request": { var person = JsonConvert.DeserializeObject <Person>(incoming); var token = person.Email.GetHashCode(); _cache.AddPersonAsync(token, person); _db.TakeAnimalAsync(person); SendJson(res, JsonConvert.SerializeObject(token)); } break; default: SendHtml(res, "Hello. Make GET /data to get json"); break; } } //SendHtml(res, $"Congratulations, {person.Name}! Pet with ID#{person.PetID} is ordered by you!"); } catch (Exception e) { Console.WriteLine(e.Message); } } }); Console.WriteLine($"\nServer is listening to http://{_ip}:{_port}..."); }