public Task StartAsync(CancellationToken cancellationToken) { if (!HttpListener.IsSupported) { Logger.LogWarning("Windows XP SP2 or Server 2003 is required to use the HttpListener class."); return(Task.CompletedTask); } listener = new HttpListener(); listener.AuthenticationSchemes = AuthenticationSchemes.Anonymous; try { listener.Prefixes.Add($"http://*:{Configuration.WebApiPort}/"); listener.Start(); } catch (System.Net.HttpListenerException ex) { Logger.LogWarning(ex, $"{ex.Message}:使用cmd命令[netsh http add urlacl url=http://*:{Configuration.WebApiPort}/ user=Everyone]"); } Logger.LogInformation($"JT808 Http Server start at {IPAddress.Any}:{Configuration.WebApiPort}."); Task.Factory.StartNew(async() => { while (listener.IsListening) { var context = await listener.GetContextAsync(); try { if (context.Request.RawUrl.StartsWith("/favicon.ico")) { context.Http404(); continue; } if (authorization.Authorization(context, out var principal)) { await ProcessRequestAsync(context, principal); } else { await context.Http401(); } } catch (AggregateException ex) { await context.Http500(); Logger.LogError(ex, ex.StackTrace); } catch (Exception ex) { await context.Http500(); Logger.LogError(ex, ex.StackTrace); } } }, cancellationToken); return(Task.CompletedTask); }
public Task StartAsync(CancellationToken cancellationToken) { if (!HttpListener.IsSupported) { Logger.LogWarning("Windows XP SP2 or Server 2003 is required to use the HttpListener class."); return(Task.CompletedTask); } listener = new HttpListener(); listener.AuthenticationSchemes = AuthenticationSchemes.Anonymous; try { listener.Prefixes.Add($"http://*:{Configuration.WebApiPort}/"); listener.Start(); } catch (System.Net.HttpListenerException ex) { Logger.LogWarning(ex, $"{ex.Message}:使用cmd命令[netsh http add urlacl url=http://*:{Configuration.WebApiPort}/ user=Everyone]"); } Logger.LogInformation($"JT808 Http Server start at {IPAddress.Any}:{Configuration.WebApiPort}."); Task.Factory.StartNew(() => { while (listener.IsListening) { var context = listener.GetContext(); try { if (context.Request.RawUrl.StartsWith("/favicon.ico")) { context.Http404(); continue; } // 增加CORS // https://stackoverflow.com/questions/25437405/cors-access-for-httplistener context.Response.AppendHeader("Access-Control-Allow-Origin", "*"); if (context.Request.HttpMethod == HttpMethod.Options.Method) { context.Response.AddHeader("Access-Control-Allow-Headers", "*"); context.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST"); context.Response.AddHeader("Access-Control-Max-Age", "1728000"); context.Http204(); } if (authorization.Authorization(context, out var principal)) { DateTime start = DateTime.Now; if (Logger.IsEnabled(LogLevel.Debug)) { Logger.LogDebug($"ProcessRequestAsync Start:{context.Request.RemoteEndPoint}-{context.Request.RawUrl}"); } ProcessRequest(context, principal); if (Logger.IsEnabled(LogLevel.Debug)) { double time = (DateTime.Now - start).TotalMilliseconds; Logger.LogDebug($"ProcessRequestAsync End:{context.Request.RemoteEndPoint}-{context.Request.RawUrl} {time}ms"); } } else { context.Http401(); } } catch (AggregateException ex) { context.Http500(); Logger.LogError(ex, ex.StackTrace); } catch (Exception ex) { context.Http500(); Logger.LogError(ex, ex.StackTrace); } } }, cancellationToken); return(Task.CompletedTask); }