Request.ServerVariables获取IP [英] Request.ServerVariables getting IP

查看:61
本文介绍了Request.ServerVariables获取IP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家早上好.

我正在尝试通过以下方式获取客户端的IP:

Good morning to all.

I am trying to get the IP of the client with:

Request.ServerVariables["REMOTE_HOST"] 
//or
Request.ServerVariables["REMOTE_ADDR"]


但我得到这样的东西:
REMOTE_ADDR = fc80::ad57:e86c:acc7:7779%10

我假设它以某种方式编码为十六进制.
如何将其转换为真实IP或直接获取IP?

那是我最初的问题,我在互联网上到处都是这样的例程,但是没人能获得像127.0.0.1这样的有意义的地址.

任何人都有更好的主意吗?

对于任何有价值的东西,
我刚刚发现fc80 :: ad57:e86c:acc7:7779%10对应于Windows 7中网络适配器属性详细信息中指定的链接本地IPv6地址". >
TIA
Pete


but I getting something like this:
REMOTE_ADDR = fc80::ad57:e86c:acc7:7779%10

I am assuming that it is encoded somehow Hex.
How can I translate that to the real IP or get the IP directly??

That was my original question, I have been getting routines here and there on the internet and none is capable to get a meaninful address like 127.0.0.1 or the like.

Anyone has a better idea?

For whatever is worth,
I just found that the fc80::ad57:e86c:acc7:7779%10 corresponds to the "Link-local IPv6 Address" specified in the network adapter properties detail in Windows 7. How can I get the IPv4 address??

TIA
Pete

推荐答案

找到了答案,
只需获取以下代码即可:
http://tutorialgenius.blogspot.com/2010/09/aspnet-get-ipv4-address-even-if-user-is.html [
Found the answer,
just get this code:
http://tutorialgenius.blogspot.com/2010/09/aspnet-get-ipv4-address-even-if-user-is.html[^]
Great code!


这是我的IP通用实用程序:
Here is my Generic Utility for IP:
/// <summary>
/// Get the current user (client) IP address
/// </summary>
/// <returns>IP address as a readable string</returns>
public static string GetUserIPAddress()
    {
    Page page = HttpContext.Current.Handler as Page;
    string ip = page.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
    if (!string.IsNullOrEmpty(ip))
        {
        string[] ipRange = ip.Split(',');
        ip = ipRange[0];
        }
    else
        {
        ip = page.Request.ServerVariables["REMOTE_ADDR"];
        }
    return ip.Trim();
    }


希望
Hope IpAddress[^] may give you an clear idea.


这篇关于Request.ServerVariables获取IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆