如果远程ip,C#httplistener错误

时间:2012-03-18 23:10:06

标签: c# webclient httplistener

我创建了一个httplistener,它只能用localhost作为前缀。如果我将其更改为远程服务器ip,则会显示错误。

以下是代码:

 HttpListener listener = new HttpListener();
        listener.Prefixes.Add("http://*:8080/");  // I need localhost replaced with remote ip
        listener.Start();
        while (true)
        {
            // Wait for a client request:
            HttpListenerContext context = listener.GetContext();

            // Respond to the request:
            string msg = "You asked for: " + context.Request.RawUrl;
            context.Response.ContentLength64 = Encoding.UTF8.GetByteCount(msg);
            context.Response.StatusCode = (int)HttpStatusCode.OK;

            using (Stream s = context.Response.OutputStream)
            using (StreamWriter writer = new StreamWriter(s))
                writer.Write(msg);
        }
        listener.Stop();

这是网络客户端:

   using (WebClient wc = new WebClient())        // Make a client request.
                    wc.DownloadString
                      ("http://"my public ip"(on  my router):8080/lg.txt"); //port forwarding is set
                MessageBox.Show("received");

仅当我将“my public ip”更改为本地IP时,它才有效 有什么想法吗?

1 个答案:

答案 0 :(得分:2)

您的Windows防火墙是否开启?尝试禁用它,看看是否有帮助。您可能需要实际禁用Windows防火墙服务(在本地服务列表中)。