HttpListener做服务器的问题 我现在用HttpListener类写了一个Http服务器,但是本机可以访问,局域网内的其它机器无法访问,请问这是怎么回事,而且我用TCPView看了一下,也没有发现程序对本地进行了相应的监听,谁能解释一下。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 class HttpServer { private HttpListener _httpListener ; private Dictionary<string,HttpListenerRequest> _requests; public HttpServer() { _httpListener = new HttpListener(); _requests = new Dictionary<string,HttpListenerRequest>(); } public bool Start() { try { string prefix = "http://localhost:80/"; _httpListener.Prefixes.Add(prefix); _httpListener.Start(); System.Threading.Thread waitThread = new System.Threading.Thread(new System.Threading.ThreadStart(WaitConnectProc)); waitThread.Start(); return true; } catch (HttpListenerException e) { return false; } } private void OnGetRequest(HttpListenerContext context) { HttpListenerRequest request = context.Request; // Obtain a response object. HttpListenerResponse response = context.Response; // Construct a response. string responseString = "<HTML><BODY> Hello world!</BODY></HTML>"; byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString); // Get a response stream and write the response to it. response.ContentLength64 = buffer.Length; System.IO.Stream output = response.OutputStream; output.Write(buffer, 0, buffer.Length); // You must close the output stream. output.Close(); _httpListener.Start(); } private void WaitConnectProc() { lock (this) { HttpListenerContext context = _httpListener.GetContext(); OnGetRequest(context); //_requests.Add(context.User.Identity.Name, context.Request); } }防火墙关闭了都没用 _httpListener.AuthenticationSchemes = AuthenticationSchemes.Anonymous; 顺便说一句,我得网络情况是这样的,主机直连到路由器上,当成HTTP服务器,然后用手机使用路由器的wifi连接主机服务器,返回的结果是找不到网页。 http://localhost:80/这个修改为 http://+:80/ 看来你没有看msdn啊。在关于httplistener类型的介绍页面,对于+和*有一段专门介绍。 localhost 应该默认是是127.0.0.1,用路由器获取的ip地址本地访问一下试试 "http://192.168.0.2:80/"; 用本机IP可以访问不...局域网访问不了,可以看下路由器的端口映射了没有,防火抢例外端口了没有, "http://192.168.0.2:80/"; 用本机IP可以访问不...局域网访问不了,可以看下路由器的端口映射了没有,防火抢例外端口了没有, .net中怎样抓取其他网页的内容 【求助】winfrom生成EXE后,运行时会意外退出。 C#中一个参数声明的格式问题 y=kx+lnk,怎么求出k 这样List<>怎么改成Arraylist 有谁知道Window服务中怎样显示一个NotifyIcon? 我用的VC#2002,不知Framework1.0可否升级,如何升级 大家帮我看看这个问题 “重新生成解决方案”和“重新生成 XXX”的疑问。 在学 C # 之前,你用的是什么语言。用什么的转过来较容易? 有没有用 PB 的? C# 程序异常显示问题 wpf 绑定问题
class HttpServer
{
private HttpListener _httpListener ;
private Dictionary<string,HttpListenerRequest> _requests;
public HttpServer()
{
_httpListener = new HttpListener();
_requests = new Dictionary<string,HttpListenerRequest>();
}
public bool Start()
{
try
{
string prefix = "http://localhost:80/";
_httpListener.Prefixes.Add(prefix);
_httpListener.Start(); System.Threading.Thread waitThread = new System.Threading.Thread(new System.Threading.ThreadStart(WaitConnectProc));
waitThread.Start();
return true;
}
catch (HttpListenerException e)
{
return false;
}
}
private void OnGetRequest(HttpListenerContext context)
{
HttpListenerRequest request = context.Request;
// Obtain a response object.
HttpListenerResponse response = context.Response;
// Construct a response.
string responseString = "<HTML><BODY> Hello world!</BODY></HTML>";
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
// Get a response stream and write the response to it.
response.ContentLength64 = buffer.Length;
System.IO.Stream output = response.OutputStream;
output.Write(buffer, 0, buffer.Length);
// You must close the output stream.
output.Close();
_httpListener.Start();
}
private void WaitConnectProc()
{
lock (this)
{
HttpListenerContext context = _httpListener.GetContext();
OnGetRequest(context);
//_requests.Add(context.User.Identity.Name, context.Request);
}
}
防火墙关闭了都没用
局域网访问不了,可以看下路由器的端口映射了没有,防火抢例外端口了没有,
局域网访问不了,可以看下路由器的端口映射了没有,防火抢例外端口了没有,