C#HttpListener异常:指定网络名称的格式无效
我使用此示例代码侦听HTTP请求:C#HttpListener异常:指定网络名称的格式无效,c#,http,network-programming,network-protocols,httplistener,C#,Http,Network Programming,Network Protocols,Httplistener,我使用此示例代码侦听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(); Ht
// 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还是我期望得到响应的机器的t的URI,我总是会得到异常:
指定网络名称的格式无效
我尝试了多种方法,其中之一是我指定了我自己电脑的URI,但得到的例外是这些位置无法访问。我研究得越多,就越迷路
在HttpListener
上的MSDN示例中,我看到URI应该也有一个端口,但是如果它必须是我正在侦听的外部机器的URI,那么我如何知道端口
此外,我还打开了防火墙中的端口(使用Windows工具,而不是commander)。我甚至关闭了Windows防火墙,但没有做任何改变
我在网络方面的知识非常有限,因此我甚至不知道我应该问的确切问题
我尝试了相关帖子的提示,例如:
但是没有任何帮助。例如,使用:
listener.Prefixes.Add("http://*:9998/");
listener.Prefixes.Add("https://*:9999/");
如果使用*listener,则会绑定到所有接口。您需要以管理员身份运行才能注册
现在,如果您在配置了IP的主机上运行此程序,例如192.168.178.39
您应该使用以下方式进行连接:要使SSL正常工作,您需要使用以下命令: netsh http add sslcert ipport=0.0.0.0:9999 certhash=Thumbprint appid={GUID} 在HttpListener上的MSDN示例中,我看到URI应该具有 也是一个端口,但是如果它必须是外部机器的URI,我是 听着,那我怎么知道港口 在这种情况下,您应该使用默认端口HTTP80或HTTPS443。您需要在计算机上使用本地IP作为前缀doNOT指定外部IP 如果网络具有NAT,则将防火墙配置为允许端口443和80上的流量到达所需主机。
现在,您可以使用外部ip和默认端口进行连接。虽然它没有完全回答我的问题,但与其他帖子一起帮助我理解了它。谢谢:)