c# - 无法侦听前缀“http://localhost:5041/”,因为它与机器上的现有注册冲突
问题描述
我的助手类,我使用 HttpListener 创建一个带有 HttpListenerRequest 的 HttpListenerResponse。但是当运行测试它有一个错误“无法监听前缀' http://localhost:5041/ '因为它与机器上的现有注册冲突”每个人都有这个问题请帮助我。谢谢。我的助手类:使用 Microsoft.Extensions.Logging;使用系统;使用 System.Net;使用 System.IO;使用 System.Net.Sockets;使用 System.Text;使用 System.Collections.Generic;
namespace ApiBusinesslogicTest { public class MockHttpResponseHelper { private readonly ILogger _logger; HttpListener _listener; int requestIndex = 0; Dictionary<int, HttpStatusCode> dHttpStatus; Dictionary<int, MemoryStream> dResponseObject; Dictionary<int, StringBuilder> dRequestData; public MockHttpResponseHelper(ILogger logger) { this._logger = logger; } public WebResponse CreateWebResponse(String prefix, Dictionary<int, HttpStatusCode> dHttpStatus, Dictionary<int, MemoryStream> dResponseObject, Dictionary<int, StringBuilder> dRequestData) { this.dRequestData = dRequestData; this.dResponseObject = dResponseObject; this.dHttpStatus = dHttpStatus; TcpListener l = new TcpListener(IPAddress.Loopback, 0); l.Start(); int port = ((IPEndPoint)l.LocalEndpoint).Port; l.Stop(); // Create a listener. // string prefix = endPoint; _listener = new HttpListener(); _listener.Prefixes.Add(prefix); _listener.Start(); try { _listener.BeginGetContext(new AsyncCallback(clientConnect), _listener); } catch (Exception e) { _logger.LogWarning(e.StackTrace); } return null; } public void stop() { _listener.Stop(); _listener.Close(); } public void clientConnect(IAsyncResult ar) { if (!_listener.IsListening) return; HttpListenerContext context = _listener.EndGetContext(ar); //_listener.BeginGetContext(clientConnect, _listener); HttpListenerRequest request = context.Request; if (!request.HasEntityBody) { _logger.LogTrace("No client data was sent with the request."); return; } System.IO.Stream body = request.InputStream; System.Text.Encoding encoding = request.ContentEncoding; System.IO.StreamReader reader = new System.IO.StreamReader(body, encoding); if (request.ContentType != null) { _logger.LogTrace("Client data content type {0}", request.ContentType); } _logger.LogTrace("Client data content length {0}", request.ContentLength64); _logger.LogTrace("Start of client data:"); // Convert the data to a string and display it on the console. string s = reader.ReadToEnd(); dRequestData[requestIndex].Append(s); _logger.LogTrace(s); _logger.LogTrace("End of client data:"); body.Close(); reader.Close(); // Obtain a response object. HttpListenerResponse response = context.Response; response.StatusCode = (int)dHttpStatus[requestIndex]; // Construct a response. if (dResponseObject != null) { byte[] buffer = dResponseObject[requestIndex].ToArray(); // Get a response stream and write the response to it. Stream output = response.OutputStream; output.Write(buffer, 0, buffer.Length); try { output.Flush(); output.Close(); response.Close(); } finally { requestIndex++; _listener.BeginGetContext(new AsyncCallback(clientConnect), _listener); } } } } }
解决方案
在 CMD 上键入“netstat -ano”以检查端口 5041 是否已在使用中(我假设)。您还可以键入“netstat -anob”,以查看哪个应用程序正在使用此端口(需要管理员权限)。
如果该端口正在使用中,您应该为您的应用程序选择另一个端口。
推荐阅读
- sequelize.js - Sequelize 按关联模型的字段排序
- java - 在可变数量的对象上使用 CompletableFuture.allOf
- ios - Swift、iOS、Seque 没有传输我想要传递的数据
- php - 尽管有 css 代码,但图像并未显示在整个屏幕上
- c# - 如何将对象传输到 Azure Blob 存储而不将文件保存在本地存储上?
- java - 我无法在 JAVA 中获得 4 位随机数
- python - TypeError 解决方案
- html - 图片未显示在移动设备上
- python - 很难理解 python 中返回和多重返回的概念
- python - 为什么 Sets 允许多个具有更改 hashCodes 的相同对象?