“指定的网络名称不再可用"在 Httplistener [英] "Specified network name is no longer available" in Httplistener
问题描述
我已经构建了一个简单的 Web 服务,它只使用 HttpListener 来接收和发送请求.有时,服务会因指定的网络名称不再可用"而失败.当我写入 HttpListenerResponse 的输出缓冲区时,它似乎被抛出.
I have built a simple web service that simply uses HttpListener to receive and send requests. Occasionally, the service fails with "Specified network name is no longer available". It appears to be thrown when I write to the output buffer of the HttpListenerResponse.
这是错误:
ListenerCallback() 错误:指定的网络名称在 System.Net.HttpResponseStream.Write(Byte[] buffer, Int32 offset, Int32 size) 中不再可用
ListenerCallback() Error: The specified network name is no longer available at System.Net.HttpResponseStream.Write(Byte[] buffer, Int32 offset, Int32 size)
这是代码的错误部分.responseString 是发送回客户端的数据:
and here is the guilty portion of the code. responseString is the data being sent back to the client:
buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
response.ContentLength64 = buffer.Length;
output = response.OutputStream;
output.Write(buffer, 0, buffer.Length);
它似乎并不总是一个巨大的缓冲区,两个例子是 3,816 字节和 142,619 字节,这些错误相隔大约 30 秒抛出.我不认为我的单个客户端应用程序会超载 HTTPlistener.客户端偶尔会突发发送/接收数据,多次交换会一个接一个地发生.
It doesn't seem to always be a huge buffer, two examples are 3,816 bytes and, 142,619 bytes, these errors were thrown about 30 seconds apart. I would not think that my single client application would be overloading HTTPlistener; the client does occasionally sent/receive data in bursts, with several exchanges happening one after another.
大多数 Google 搜索显示这是一个常见的 IT 问题,当出现网络问题时,会显示此错误 - 大多数帮助是针对系统管理员诊断应用程序问题而不是开发人员追踪错误.我的应用已经在不同的机器、网络等上进行了测试,我认为这不仅仅是一个网络配置问题.
Mostly Google searches shows that this is a common IT problem where, when there are network problems, this error is shown -- most of the help is directed toward sysadmins diagnosing a problem with an app moreso than developers tracking down a bug. My app has been tested on different machines, networks, etc. and I don't think it's simply a network configuration problem.
这个问题的原因可能是什么?
What may be the cause of this problem?
推荐答案
我也遇到了这个问题,当指定了 ContentLength64
并且 KeepAlive
为 false代码>.似乎客户端正在检查
Content-Length
标头(所有可能的帐户都正确设置了该标头,因为我得到任何其他值的异常)然后说Whelp I'm完成 KTHXBYE" 并在底层 HttpListenerResponse
流期望它之前关闭连接.目前,我只是捕捉异常并继续前进.
I'm getting this too, when a ContentLength64
is specified and KeepAlive
is false
. It seems as though the client is inspecting the Content-Length
header (which, by all possible accounts, is set correctly, since I get an exception with any other value) and then saying "Whelp I'm done KTHXBYE" and closing the connection a little bit before the underlying HttpListenerResponse
stream was expecting it to. For now, I'm just catching the exception and moving on.
这篇关于“指定的网络名称不再可用"在 Httplistener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!