HttpListener问题,局域网内用浏览器无法显示该页面

webjqx 2009-04-10 11:57:51
HttpListener实现的web服务器,我的ip为192.168.0.8,我绑定了8080端口,在本机上我用ie可以访问,但在另一台机器上访问就不行了,我将端口去掉或改为80,就可以访问,不知道为什么?很郁闷。

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;


namespace HttpListener_ex
{
class Program
{
static void Main(string[] args)
{
using (HttpListener listerner = new HttpListener())
{
listerner.AuthenticationSchemes = AuthenticationSchemes.Anonymous;//指定身份验证 Anonymous匿名访问
listerner.Prefixes.Add("http://*:8080/web/");
//listerner.Prefixes.Add("http://127.0.0.1:8080/web/");
//listerner.Prefixes.Add("http://192.168.0.33:8080/web/");
//listerner.Prefixes.Add("http://192.168.0.166:8080/web/");


// listerner.Prefixes.Add("http://localhost/web/");
listerner.Start();
Console.WriteLine("WebServer Start Successed.......");
while (true)
{
//等待请求连接
//没有请求则GetContext处于阻塞状态
HttpListenerContext ctx = listerner.GetContext();
ctx.Response.StatusCode = 200;//设置返回给客服端http状态代码
string name = ctx.Request.QueryString["name"];


if (name != null)
{
Console.WriteLine(name);
}




//使用Writer输出http响应代码
using (StreamWriter writer = new StreamWriter(ctx.Response.OutputStream))
{
Console.WriteLine("hello");
writer.WriteLine("<html><head><title>The WebServer Test</title></head><body>");
writer.WriteLine("<div style=\"height:20px;color:blue;text-align:center;\"><p> hello {0}</p></div>", name);
writer.WriteLine("<ul>");


foreach (string header in ctx.Request.Headers.Keys)
{
writer.WriteLine("<li><b>{0}:</b>{1}</li>", header, ctx.Request.Headers[header]);

}
writer.WriteLine("</ul>");
writer.WriteLine("</body></html>");


writer.Close();
ctx.Response.Close();
}


}
listerner.Stop();
}

}
}
}
...全文
853 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 iland9876543210 的回复:]

防火墙问题,安全中心将8080设为授信端口
[/Quote]

是这样的。
slzlixu 2011-02-22
  • 打赏
  • 举报
回复
有时也可能是端口在路由器没映射到内网IP上.
iland9876543210 2010-04-23
  • 打赏
  • 举报
回复
防火墙问题,安全中心将8080设为授信端口
战争迷雾 2010-01-26
  • 打赏
  • 举报
回复
我也遇到类似问题,顶

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧