【发布时间】:2018-11-24 01:15:03
【问题描述】:
我正在使用这个示例代码来监听 HTTP 请求:
// Create a listener.
HttpListener listener = new HttpListener();
// Add the prefixes.
listener.Prefixes.Add("some URI");
listener.Start();
Console.WriteLine("Listening...");
HttpListenerContext context = listener.GetContext();
HttpListenerRequest request = context.Request;
HttpListenerResponse response = context.Response;
listener.Stop();
但我现在过得很艰难,因为我不知道前缀应该是什么。我已经阅读了这方面的文档,但是无论我尝试在那里输入什么,无论是我的计算机的 URI 还是我希望得到响应的机器的 URI,我总是会得到异常:
指定的网络名称格式无效
我尝试了多种方法,其中之一是我指定了我自己 PC 的 URI,但出现了无法访问这些位置的异常。我研究得越多,我迷失的越多。
在HttpListener 的 MSDN 示例中,我看到 URI 应该也有一个端口,但如果它必须是我正在监听的外部机器的 URI,那么我怎么知道端口?
另外,我在防火墙中打开了端口(使用 Windows 工具,而不是指挥官)。我什至关闭了 Windows 防火墙,但没有改变任何东西。
我对网络的了解非常有限,因此我什至不知道我应该问的确切问题。
我尝试了相关帖子的提示,例如:
C# HttpListener The format of the specified network name is not valid
但没有任何帮助。
【问题讨论】:
标签: c# http network-programming network-protocols httplistener