简体   繁体   English

在服务器上获取请求客户端的IP地址

[英]Getting requesting client's IP address on server

I have a HttpListener where I'm interested in being able to see the IP address of the requesting clients. 我有一个HttpListener,在这里我有兴趣看到请求客户端的IP地址。 A bonus would also being able to see the DNS of the client, but I'm not sure how that would be possible since as far as I know that information is not being sent with HTTP? 另外,还能看到客户端的DNS,但我不确定怎么办,因为据我所知,信息不是通过HTTP发送的?

Anyway, as far as I can see, I should be able to use Request.UserHostAddress for this, but I'm just getting my local IP address. 无论如何,据我所知,我应该能够使用Request.UserHostAddress ,但是我只是获得了本地IP地址。 What am I doing wrong here? 我在这里做错了什么?

Where I should get the client IP. 我应该在哪里获取客户端IP。

        HttpListenerContext context = listener.EndGetContext(result);
        string clientName = context.Request.UserHostAddress;

Where I'm writing out on a server output listbox I have: 我在服务器输出列表框中写出的位置有:

        public static void TileString(int x, int y, int z, string dbName, string clientName)
        {
        int[] tileInts = { z, x, y };
        string tileString = string.Join("/", tileInts);

        Application.Current.Dispatcher.Invoke(new Action(() =>
        {
            var mainWindow = Application.Current.MainWindow as MainWindow;
            mainWindow.AppendServerOutput("Delivering tile " + tileString + " in format [z, x, y]" + " from " + dbName + " to client " + clientName + "\n");
        }));
        }

尝试使用:

string clientIP = context.Request.RemoteEndPoint.ToString());

From the MSDN documentation for the HttpListenerRequest.UserHostAddress property: 从MSDN文档的HttpListenerRequest.UserHostAddress属性:

Gets the server IP address and port number to which the request is directed. 获取请求定向到的服务器IP地址和端口号。

In other words, it's not the remote end-point's address. 换句话说,它不是远程端点的地址。 It's the address of the server that the remote end-point used. 它是远程端点使用的服务器的地址。

As you have seen, you can use the RemoteEndPoint to retrieve the IP address of the remote end-point. 如您所见,您可以使用RemoteEndPoint检索远程端点的IP地址。

Use the System.Net.Dns.GetHostEntry() method to do reverse-DNS lookups (ie retrieve the remote host name for the IP address). 使用System.Net.Dns.GetHostEntry()方法进行反向DNS查找(即,检索IP地址的远程主机名)。

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

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