我现在用HttpListener类写了一个Http服务器,但是本机可以访问,局域网内的其它机器无法访问,请问这是怎么回事,而且我用TCPView看了一下,也没有发现程序对本地进行了相应的监听,谁能解释一下。

解决方案 »

  1.   


     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);
                }
            }
    防火墙关闭了都没用
      

  2.   

      _httpListener.AuthenticationSchemes = AuthenticationSchemes.Anonymous;
      

  3.   

    顺便说一句,我得网络情况是这样的,主机直连到路由器上,当成HTTP服务器,然后用手机使用路由器的wifi连接主机服务器,返回的结果是找不到网页。
      

  4.   

    http://localhost:80/这个修改为     http://+:80/
      

  5.   

    看来你没有看msdn啊。在关于httplistener类型的介绍页面,对于+和*有一段专门介绍。
      

  6.   

    localhost 应该默认是是127.0.0.1,用路由器获取的ip地址本地访问一下试试
      

  7.   

    "http://192.168.0.2:80/";  用本机IP可以访问不...
    局域网访问不了,可以看下路由器的端口映射了没有,防火抢例外端口了没有,
      

  8.   

    "http://192.168.0.2:80/";  用本机IP可以访问不...
    局域网访问不了,可以看下路由器的端口映射了没有,防火抢例外端口了没有,