Node-fetch连接本地C# HttpListener服务器遇ECONNREFUSED错误
问题:node-fetch调用本地C# HttpListener服务报ECONNREFUSED错误
Linux环境下,通过浏览器访问本地9000端口的C# HttpListener服务器可正常返回200状态码,但将浏览器中使用的URL直接复制到node-fetch中调用时,却抛出ECONNREFUSED错误。
node-fetch调用代码
await fetch(`http://localhost:9000/renderAvatar.3d?pant=http://localhost/assets/2.png&face=http://localhost/assets/0.png&shirt=http://localhost/assets/1.png&name=de&trick=e`)
C#服务器核心代码
HttpListener httpServer; public void SomeFunction() { //Some boilerplate for http server definitely not stolen amogus httpServer = new HttpListener(); httpServer.Prefixes.Add("http://localhost:9000/"); httpServer.Start(); Task demo = Server(); demo.GetAwaiter().GetResult(); } public async Task server() { while(true) { HttpListenerContext ctx = await httpServer.GetContextAsync(); HttpListenerRequest req = ctx.Request; HttpListenerResponse resp = ctx.Response; if(req.HttpMethod == "GET") { if(req.Url.AbsolutePath.ToString() == "/renderAvatar.3d") { var parameters = HttpUtility.ParseQueryString(req.Url.Query); if(parameters.Count > 3) { var pant = parameters[0].ToString(); var face = parameters[1].ToString(); var shirt = parameters[2].ToString(); var name = parameters[3].ToString(); // 因属专有代码,无法提供更多内容
错误信息
FetchError: request to http://127.0.0.1:9000/renderAvatar.3d?pant=http://localhost/assets/2.png&face=http://localhost/assets/0.png&shirt=http://localhost/assets/1.png&name=de&trick=e failed, reason: connect ECONNREFUSED 127.0.0.1:9000 at ClientRequest.<anonymous> (file:///home/(confidental)/backend/node_modules/node-fetch/src/index.js:108:11) at ClientRequest.emit (node:events:527:28) at Socket.socketErrorListener (node:_http_client:454:9) at Socket.emit (node:events:527:28) at emitErrorNT (node:internal/streams/destroy:157:8) at emitErrorCloseNT (node:internal/streams/destroy:122:3) at processTicksAndRejections (node:internal/process/task_queues:83:21) { type: 'system', errno: 'ECONNREFUSED', code: 'ECONNREFUSED', erroredSysCall: 'connect' }
当前临时解决方案是通过程序调用Chrome访问后关闭,但内存占用过高,寻求合理解决办法。(注:9000端口未被防火墙限制)
内容的提问来源于stack exchange,提问作者drk1
相关产品推荐
相关产品推荐

