简单的Web服务器:指定的网络名的格式无效 [英] Simple Web Server: The format of the specified network name is invalid
问题描述
我在与我写一个简单的Web服务器有问题。我需要能够通过本地主机和IP连接到该服务器。不过,我有通过IP连接问题。这里是我的代码:
I'm having a problem with a simple web server that I am writing. I need to be able to connect to the server through localhost and IP. However, I am having problems connecting through IP. Here is my code:
private void start_button_Click(object sender, EventArgs e)
{
start_button.Text = "Listening...";
HttpListener server = new HttpListener();
server.Prefixes.Add("http://201.0.0.10:69/");
server.Prefixes.Add("http://localhost:69/");
server.Start();
while (true)
{
HttpListenerContext context = server.GetContext();
HttpListenerResponse response = context.Response;
string page = Directory.GetCurrentDirectory() +
context.Request.Url.LocalPath;
if (page == string.Empty)
page = page + "index.html";
TextReader tr = new StreamReader(page);
string msg = tr.ReadToEnd();
byte[] buffer = Encoding.UTF8.GetBytes(msg);
response.ContentLength64 = buffer.Length;
Stream st = response.OutputStream;
st.Write(buffer, 0, buffer.Length);
context.Response.Close();
}
}
我不断收到此错误:指定的格式。网名是无效的。
I keep getting this error: The format of the specified network name is invalid.
我知道我的问题就在于此位:
I know my problem lies in this bit:
server.Prefixes.Add("http://201.0.0.10:69/");
我可以通过本地主机连接,如果我注释掉这一行。
I can connect through localhost if I comment out this line.
有谁知道我可能是做错了?
Does anyone know what I could be doing wrong?
好吧,我得到了IP地址的工作,但我现在有这个线路有问题:
Okay, I got the IP adress working, but now I'm having a problem with this line:
if (page == string.Empty)
page = page + "index.html";
由于某些原因,它没有增加的index.html到最后。
For some reason, it's not adding index.html to the end.
推荐答案
除了设置绑定在 application.config
文件,你可能需要设置你的系统通过运行这个命令来侦听来自某些IP地址的http:
As well as setting the bindings in the application.config
file you may need to set your system to listen for http from certain IP addresses by running this command:
netsh http add iplisten 201.0.0.10
您可能还需要加上本地主机:
You may also need to add localhost:
netsh http add iplisten 127.0.0.1
和在其他的答案中提到这些内容添加到绑定文件:
And as mentioned in other answers add these to the bindings file:
<binding protocol="http" bindingInformation="*:69:201.0.0.10" />
<binding protocol="http" bindingInformation="*:69:localhost" />
这篇关于简单的Web服务器:指定的网络名的格式无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!