简体   繁体   English

使用httpListener创建以URL为我的IP地址的自定义网站

[英]using httpListener to create custom website with URL as my IP-Address

I want to create a website with URL as my IP-address [ex: 192.XXX] 我想创建一个以URL作为我的IP地址的网站[例如:192.XXX]

That website would respond with a " HELLO THERE " message to any user who accesses my URL. 该网站将向访问我的URL的任何用户显示“ HELLO THERE ”消息。

I use the following code to do this![its just a basic code with no threading] 我使用以下代码来做到这一点![它只是没有线程的基本代码]

class listenToHTTP
{
    HttpListener _listner;
    public void start()
    {
        _listner = new HttpListener();
        _listner.Prefixes.Add("http://localhost/");//default port 80
        _listner.Start();
    }
    public void process()
    {
         while (true)
         {
            HttpListenerContext context = _listner.GetContext();
            byte[] output = Encoding.ASCII.GetBytes("HELLO THERE");
            context.Response.ContentEncoding = Encoding.ASCII;
            context.Response.ContentLength64 = output.Length;
            context.Response.OutputStream.Write(output, 0, output.Length);

          }
     }
}

The problem is that i dont know the IP-address through which anyone would access. 问题是我不知道任何人都可以通过其访问的IP地址

It perfectly shows the response "HELLO THERE" when I use http://localhost/ as URL. 当我使用http:// localhost /作为URL时,它完美地显示了响应“ HELLO THERE”。

But what IP-address would other people use so that they can access my simple website. 但是其他人将使用什么IP地址 ,以便他们可以访问我的简单网站。

I have tried my IP-address in the browser but it doesnt work . 我已经在浏览器中尝试过我的IP地址,但是它不起作用

There are two things to look out for when doing this; 执行此操作时需要注意两点:

  • If you listen to a localhost address, only localhost will be able to connect to your HttpListener. 如果您监听本地主机地址,则只有本地主机才能连接到HttpListener。 You'll need to add a prefix with http://192.XXX/ (where the 192.XXX is your local IP of course) and listen to that. 您需要添加带有http://192.XXX/的前缀(当然,其中192.XXX是您的本地IP)并收听。 That may (depending on your operating system) require you to run as admin, at least if you want to do it on a port < 1024. You can test if this works by connecting to your IP# from your local machine instead of a localhost address. 这可能(取决于您的操作系统)要求您以admin身份运行,至少要在<1024端口上进行。您可以通过从本地计算机(而不是本地主机)连接到IP#来测试其是否有效地址。

  • If you're running Windows, the firewall may get in the way. 如果您正在运行Windows,则防火墙可能会妨碍您。 If it seems to (ie you can connect to your IP# from the local machine but nothing else can connect) you'll need to open the port manually. 如果看起来(例如,您可以从本地计算机连接到IP#,但没有其他连接可以连接),则需要手动打开端口。 There are plenty of guides how to do this on Google. 在Google上有很多指南。

@Joachim reply is already good enough. @Joachim的回复已经足够好了。 I will like to add a bit more... 我想补充一点...

  1. You need to expose the above mentioned IP address publicly to get the URL accessible to others. 您需要公开公开上述IP地址,以使其他人可以访问该URL。
  2. In case of Exposing the URL to your domain only (ie in case of Intranet), check with your System Administrator to configure the IP Address on Intranet. 如果仅将URL公开给您的域(即Intranet),请与系统管理员联系以在Intranet上配置IP地址。
  3. Localhost settings accessibility is confined to your machine only. Localhost设置的可访问性仅限于您的计算机。
  4. Make sure to check the firewall constraint for the URL accessibility to implement Point 1 or 2 确保检查防火墙约束的URL可访问性以实现点1或2

For more information check the reference for HTTPListener 有关更多信息,请参见HTTPListener的参考。

  1. HTTPListener HTTPListener
  2. HTTPListener HTTPListener
  3. HTTPListener HTTPListener

The problem was that I was referring to a private network address which are local to the network and cannot be accessed by anyone outside that private network .. 问题是我指的是private network地址,该地址是network local ,该private network 外部的任何人都无法访问..

These are the range of ip-address that are used in private network and so systems with this address cannot be a server or host a website.. 这些是专用网络中使用的ip-address范围,因此具有该地址的系统不能是服务器或托管网站。

10.0.0.0 to 10.255.255.255

172.16.0.0 to 172.31.255.255

192.168.0.0 to 192.168.255.255

You should use a public address.. 您应该使用公共地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM