我有一个HttpListener,我有兴趣能够看到请求客户端的IP地址。奖金也可以看到客户端的DNS,但我不确定这是怎么可能的,因为据我所知,信息没有用HTTP发送?
无论如何,据我所知,我应该可以使用Request.UserHostAddress
,但我只是获取本地IP地址。我在这里做错了什么?
我应该在哪里获得客户端IP。
HttpListenerContext context = listener.EndGetContext(result);
string clientName = context.Request.UserHostAddress;
我在服务器输出列表框中写出的内容是:
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");
}));
}
答案 0 :(得分:5)
尝试使用:
string clientIP = context.Request.RemoteEndPoint.ToString());
答案 1 :(得分:1)
来自HttpListenerRequest.UserHostAddress
属性的MSDN文档:
获取请求所针对的服务器IP地址和端口号。
换句话说,它不是远程终点的地址。它是远程端点使用的服务器的地址。
如您所见,您可以使用RemoteEndPoint
来检索远程终端的IP地址。
使用System.Net.Dns.GetHostEntry()
方法执行反向DNS查找(即检索IP地址的远程主机名)。