/// <summary> /// 启动WIFI连接侦听器 /// </summary> public async Task StartToListenRequest() { if (IsListeningThreadWorking) { return; } if (IsDisposed) { throw new ObjectDisposedException("This Object has been disposed"); } IsListeningThreadWorking = true; try { Listener.Start(); while (true) { HttpListenerContext Context = await Listener.GetContextAsync().ConfigureAwait(false); _ = Task.Factory.StartNew((Para) => { try { HttpListenerContext HttpContext = Para as HttpListenerContext; if (HttpContext.Request.Url.LocalPath.Substring(1) == FilePathMap.Key) { if (FileSystemStorageItemBase.Open(FilePathMap.Value, ItemFilters.File) is FileSystemStorageItemBase ShareFile) { using (FileStream Stream = ShareFile.GetFileStreamFromFile(AccessMode.Read)) { try { Context.Response.AddHeader("Pragma", "No-cache"); Context.Response.AddHeader("Cache-Control", "No-cache"); Context.Response.AddHeader("Content-Disposition", $"Attachment;filename={Uri.EscapeDataString(ShareFile.Name)}"); Context.Response.ContentLength64 = Stream.Length; Context.Response.ContentType = "application/octet-stream"; Stream.CopyTo(Context.Response.OutputStream); } catch (HttpListenerException ex) { LogTracer.Log(ex); } finally { Context.Response.Close(); } } } } else { string ErrorMessage = $"<html><head><title>Error 404 Bad Request</title></head><body><p style=\"font-size:50px\">HTTP ERROR 404</p><p style=\"font-size:40px\">{Globalization.GetString("WIFIShare_Error_Web_Content")}</p></body></html>"; Context.Response.StatusCode = 404; Context.Response.StatusDescription = "Bad Request"; Context.Response.ContentType = "text/html"; Context.Response.ContentEncoding = Encoding.UTF8; using (StreamWriter Writer = new StreamWriter(Context.Response.OutputStream, Encoding.UTF8)) { Writer.Write(ErrorMessage); } Context.Response.Close(); } } catch (Exception e) { LogTracer.Log(e); ThreadExitedUnexpectly?.Invoke(this, e); } }, Context, Cancellation.Token, TaskCreationOptions.LongRunning, TaskScheduler.Current); } } catch (ObjectDisposedException) { IsListeningThreadWorking = false; } catch (Exception e) { IsListeningThreadWorking = false; ThreadExitedUnexpectly?.Invoke(this, e); } finally { Cancellation?.Dispose(); Cancellation = null; } }