ContentType类型参考 https://www.runoob.com/http/http-content-type.html

    作者:ludewig 链接:https://blog.csdn.net/lordwish/article/details/86615077

    只是实现了Get的部分资源访问,用于展示简单的网页,解决JS需要IIS的跨域问题,在不需要搭建IIS的情况下显示内容
    C#代码

    1. using System;
    2. using System.Collections.Generic;
    3. using System.IO;
    4. using System.Linq;
    5. using System.Net;
    6. using System.Text;
    7. namespace TestHttpListener
    8. {
    9. class Program
    10. {
    11. /// <summary>
    12. /// 网页项目地址,最后需要加"\"
    13. /// </summary>
    14. static string path = @"网页项目地址";
    15. /// <summary>
    16. /// URL
    17. /// </summary>
    18. static string url = "http://localhost:8899/";
    19. static void Main(string[] args)
    20. {
    21. HttpListener listener = new HttpListener();
    22. listener.AuthenticationSchemes = AuthenticationSchemes.Anonymous;
    23. listener.Prefixes.Add(url);
    24. listener.Start();
    25. listener.BeginGetContext(ListenerHandle, listener);
    26. Console.ReadKey();
    27. listener.Stop();
    28. listener.Close();
    29. }
    30. private static void ListenerHandle(IAsyncResult result)
    31. {
    32. HttpListener listener = result.AsyncState as HttpListener;
    33. if (listener == null)
    34. {
    35. return;
    36. }
    37. if (listener.IsListening)
    38. {
    39. HttpListenerContext context = listener.EndGetContext(result);
    40. listener.BeginGetContext(ListenerHandle, result.AsyncState);
    41. //解析Request请求
    42. HttpListenerRequest request = context.Request;
    43. switch (request.HttpMethod)
    44. {
    45. case "POST":
    46. break;
    47. case "GET":
    48. string filePath = string.Empty;
    49. if (request.RawUrl=="/")
    50. {
    51. /*主页*/
    52. filePath = Path.Combine(path, "index.html");
    53. }
    54. else
    55. {
    56. filePath = Path.Combine(path, request.RawUrl.Replace('/', '\\').TrimStart('\\'));
    57. }
    58. using (HttpListenerResponse response = context.Response)
    59. {
    60. response.ContentEncoding = Encoding.UTF8;
    61. byte[] bytes = null;
    62. if (File.Exists(filePath))
    63. {
    64. response.StatusCode = 200;
    65. string suffixName = new FileInfo(filePath).Extension;
    66. response.ContentType = $"{GetContentType(suffixName)};charset=UTF-8";
    67. bytes = File.ReadAllBytes(filePath);
    68. }
    69. else
    70. {
    71. response.StatusCode = 200;
    72. response.ContentType = "text/html;charset=UTF-8";
    73. bytes = Encoding.UTF8.GetBytes("404");
    74. }
    75. using (Stream stream = response.OutputStream)
    76. {
    77. stream.Write(bytes, 0, bytes.Length);
    78. }
    79. }
    80. break;
    81. }
    82. }
    83. }
    84. /// <summary>
    85. /// 获取内容类型
    86. /// 参考:https://www.runoob.com/http/http-content-type.html
    87. /// </summary>
    88. /// <param name="suffixName">文件后缀名称</param>
    89. /// <returns>内容类型</returns>
    90. private static string GetContentType(string suffixName)
    91. {
    92. string name = suffixName.ToLower();
    93. switch (name)
    94. {
    95. case ".html":
    96. return "text/html";
    97. case ".jpeg":
    98. case ".jpg":
    99. return "image/jpeg";
    100. case ".js":
    101. return "application/x-javascript";
    102. case ".css":
    103. return "text/css";
    104. case ".ico":
    105. return "image/x-icon";
    106. case ".json":
    107. return "application/json";
    108. default:
    109. break;
    110. }
    111. return "text/plain";
    112. }
    113. }
    114. }