This provides a local HTTP server on device. It allows the app to create a server by defining routes for given methods.
nuget install Float.HttpServerusingSystem;usingSystem.Collections.Generic;usingSystem.Net;usingFloat.HttpServer;usingXamarin.Essentials;usingstaticFloat.HttpServer.HttpRouter;usingHttpServer=Float.HttpServer.LocalHttpServer;publicclassLocalHttpServer{privatereadonlyHttpServerserver;privateUriuri;/// <summary>/// Initializes a new instance of the <see cref="LocalHttpServer"/> class./// </summary>publicLocalHttpServer(){varhost="127.0.0.1";varport=33616;server=newHttpServer(host,port);uri=newUri($"http://{host}:{port}/");// If the route is not matched directly, always use the StaticFileResponder.server.SetDefaultResponder(newStaticFileResponder(FileSystem.CacheDirectory));// Create an error page when things aren't found.server.SetErrorResponder(newErrorResponder404());// Inject some middleware that stops the request if the user-agent// doesn't contain the word "Mozilla".server.Use(newList<HttpMethod>{HttpMethod.GET},(HttpListenerRequestrequest,refHttpListenerResponseresponse)=>{if(request.Headers.HasKeys()&&string.Join(string.Empty,request.Headers.GetValues("User-Agent")).Contains("Mozilla")==false){response.StatusCode=401;returnfalse;}returntrue;});// Create a dynamic route that responds to post// requests at /node/:nodeId where nodeId is a parameter injected// into the responder.server.Post("/node/:nodeId",newNodePostResponder());// Create a route that responds to GET, PUT, and POST requests// at /agents/profile.server.AddResponser(newList<HttpMethod>{HttpMethod.GET,HttpMethod.POST,HttpMethod.PUT,},"/agents/profile",newAgentProfileResponder());});/// <summary>/// Starts the default server./// </summary>publicvoidStart(){server.Start();}/// <summary>/// Stops the default server./// </summary>publicvoidStop(){server.Stop();}}varlocalHttpServer=newLocalHttpServer();localHttpServer.Start();localHttpServer.Stop()./build.sh