private void DevicePortalConnection_RequestReceived(DevicePortalConnection sender, DevicePortalConnectionRequestReceivedEventArgs args) { var req = args.RequestMessage; var res = args.ResponseMessage; if (req.RequestUri.AbsolutePath.ToString() == statusUri.ToString()) { args.ResponseMessage.StatusCode = HttpStatusCode.Ok; MemoryStream mem = new MemoryStream(); serializer.WriteObject(mem, AppSettings.Current); mem.Position = 0; args.ResponseMessage.Content = new HttpStringContent(new StreamReader(mem).ReadToEnd()); args.ResponseMessage.Content.Headers.ContentType = new HttpMediaTypeHeaderValue("application/json"); return; } if (req.RequestUri.AbsolutePath.ToString() == updateUri.ToString()) { WwwFormUrlDecoder decoder = new WwwFormUrlDecoder(req.RequestUri.Query); AppSettings.Current.DARKSKY_API_KEY = decoder.GetFirstValueByName("DARKSKY_API_KEY"); AppSettings.Current.RAIL_API_KEY = decoder.GetFirstValueByName("RAIL_API_KEY"); AppSettings.Current.STATION_CRS = decoder.GetFirstValueByName("STATION_CRS"); AppSettings.Current.LOCATION_LAT = double.Parse(decoder.GetFirstValueByName("LOCATION_LAT")); AppSettings.Current.LOCATION_LNG = double.Parse(decoder.GetFirstValueByName("LOCATION_LNG")); AppSettings.Current.GET_DRINK_TIME = int.Parse(decoder.GetFirstValueByName("GET_DRINK_TIME")); AppSettings.Current.DRINK_UP_TIME = int.Parse(decoder.GetFirstValueByName("DRINK_UP_TIME")); AppSettings.Current.WALK_TIME_TO_STATION = int.Parse(decoder.GetFirstValueByName("WALK_TIME_TO_STATION")); AppSettings.Current.Save(); args.ResponseMessage.StatusCode = HttpStatusCode.Ok; args.ResponseMessage.Content = new HttpStringContent("{ \"status\": \"good\" }"); args.ResponseMessage.Content.Headers.ContentType = new HttpMediaTypeHeaderValue("application/json"); return; } if (req.RequestUri.LocalPath.ToLower().Contains("/www/") || req.RequestUri.LocalPath.ToLower().EndsWith("/")) { var filePath = req.RequestUri.AbsolutePath.Replace('/', '\\').ToLower(); //filePath = filePath.Replace("\\departureboard", ""); if (filePath.EndsWith(@"\")) { filePath += @"\www\index.html"; } try { var fileStream = Windows.ApplicationModel.Package.Current.InstalledLocation.OpenStreamForReadAsync(filePath).GetAwaiter().GetResult(); res.StatusCode = HttpStatusCode.Ok; res.Content = new HttpStreamContent(fileStream.AsInputStream()); res.Content.Headers.ContentType = new HttpMediaTypeHeaderValue("text/html"); } catch (FileNotFoundException e) { string con = String.Format("<h1>{0} - not found</h1>\r\n", filePath); con += "Exception: " + e.ToString(); res.Content = new HttpStringContent(con); res.StatusCode = HttpStatusCode.NotFound; res.Content.Headers.ContentType = new HttpMediaTypeHeaderValue("text/html"); } return; } args.ResponseMessage.StatusCode = HttpStatusCode.NotFound; }
// Sample RequestReceived handler demonstrating response construction, based on request private void DevicePortalConnection_RequestReceived(DevicePortalConnection sender, DevicePortalConnectionRequestReceivedEventArgs args) { if (args.RequestMessage.RequestUri.AbsolutePath.ToString() == statusUri.ToString()) { args.ResponseMessage.StatusCode = HttpStatusCode.Ok; args.ResponseMessage.Content = new HttpStringContent("{ \"status\": \"good\" }"); args.ResponseMessage.Content.Headers.ContentType = new HttpMediaTypeHeaderValue("application/json"); } else { args.ResponseMessage.StatusCode = HttpStatusCode.NotFound; } }
private void DevicePortalConnection_RequestReceived(DevicePortalConnection sender, DevicePortalConnectionRequestReceivedEventArgs args) { var req = args.RequestMessage; var res = args.ResponseMessage; if (req.RequestUri.AbsolutePath.EndsWith("/echo")) { // construct an html response message string con = "<h1>" + req.RequestUri.AbsoluteUri + "</h1><br/>"; var proc = Windows.System.Diagnostics.ProcessDiagnosticInfo.GetForCurrentProcess(); con += String.Format("This process is consuming {0} bytes (Working Set)<br/>", proc.MemoryUsage.GetReport().WorkingSetSizeInBytes); con += String.Format("The process PID is {0}<br/>", proc.ProcessId); con += String.Format("The executable filename is {0}", proc.ExecutableFileName); res.Content = new HttpStringContent(con); res.Content.Headers.ContentType = new HttpMediaTypeHeaderValue("text/html"); res.StatusCode = HttpStatusCode.Ok; } //... }