private void HandleGet(HttpListenerContext context) { // no get vem tudo na URL // Extrai o UID (http://server.com/UID) var request = context.Request; var reqPath = request.Url.GetComponents(UriComponents.Path, UriFormat.SafeUnescaped); // Pega as medidas do usuario no DB int uid; dynamic resp = (Int32.TryParse(reqPath, out uid)) ? MedidasDB.GetMedidas(uid) : null; // Serializa em JSON if (resp != null) { var list = new List <string>(); foreach (Message message in resp) { list.Add(message.ToJson()); } resp = JsonConvert.SerializeObject(list); this.SendResponse(context.Response, resp); } else { this.SendResponse(context.Response, resp, 400); } }
private void HandlePost(HttpListenerContext context) { string body = new StreamReader(context.Request.InputStream).ReadToEnd(); Console.WriteLine("[POST] Recebeu: " + body); try { var msg = Message.ToMessage(body); Console.WriteLine(msg.ToString()); MedidasDB.AddMessage(msg); // MedidasDB.AddMessage(Message.ToMessage(body)); SendResponse(context.Response, "ACK"); } catch (Exception e) { SendResponse(context.Response, "NACK", 400); } }