如何获得用户PC IP地址 [英] HOw to get User PC IP Address

查看:76
本文介绍了如何获得用户PC IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的评级功能,我需要获取用户的System(PC)IP地址,而不是c#中的N/W提供者IP.
我尝试了以下操作,但没有成功:

string host = Dns.GetHostName();
        //IPHostEntry local = Dns.GetHostByName(host);
        //IPAddress ipadd = local.AddressList[0];



 字符串 ipAddress = Request.ServerVariables [" ];
        如果(ipAddress ==  || ipAddress == " ")
        {
            ipAddress = Request.ServerVariables [" ];
        } 



请尽快帮我

谢谢

解决方案

如果您想要外部IP地址,请使用外部服务:

http://www.whatismyip.com/ [ ^ ]

将获取请求发送到上面的URL,并删除IP地址,甚至从页面标题中获取IP地址:

(?< =< TITLE>.*)\ d * \.\ d * \.\ d * \.\ d *(?=</TITLE>)


对于想要的人来说还可以:

using System.Net;
using System.Text;

namespace DreamInCode.Snippets
{
    public static class IPFinder
    {
        private static readonly UTF8Encoding utf8 = new UTF8Encoding();

        public static IPAddress ExternalIPAddress
        {
            get
            {
                string whatIsMyIp = "http://automation.whatismyip.com/n09230945.asp";
                WebClient wc = new WebClient();
                string response = utf8.GetString(wc.DownloadData(whatIsMyIp));
                IPAddress myIPAddress = IPAddress.Parse(response);

                return myIPAddress;
            }
        }
    }
}



我的来源: http://www.dreamincode.net/code/snippet959.htm [ 公共 字符串 GetIP() { 字符串 Str = " ; Str = System.Net.Dns.GetHostName(); IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(Str); IPAddress [] addr = ipEntry.AddressList; 返回 addr [addr.Length- 1 ].ToString(); }


使用C#获取客户端的真实IP地址 [ ^ ]


For my rating functionality i need to get the User''s System(PC) IP address and not the N/W provider IP in c#.
I tried following but not worked:

string host = Dns.GetHostName();
        //IPHostEntry local = Dns.GetHostByName(host);
        //IPAddress ipadd = local.AddressList[0];



string ipAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
        if (ipAddress == null || ipAddress == "")
        {
            ipAddress = Request.ServerVariables["REMOTE_ADDR"];
        }



please help me ASAP

Thanks

解决方案

If you want your external IP address, use an external service:

http://www.whatismyip.com/[^]

Send a get request to the URL above and strip out the IP address, or even get the IP address from the Title of the page:

(?<=<TITLE>.*)\d*\.\d*\.\d*\.\d*(?=</TITLE>)



OK for those who want it:

using System.Net;
using System.Text;

namespace DreamInCode.Snippets
{
    public static class IPFinder
    {
        private static readonly UTF8Encoding utf8 = new UTF8Encoding();

        public static IPAddress ExternalIPAddress
        {
            get
            {
                string whatIsMyIp = "http://automation.whatismyip.com/n09230945.asp";
                WebClient wc = new WebClient();
                string response = utf8.GetString(wc.DownloadData(whatIsMyIp));
                IPAddress myIPAddress = IPAddress.Parse(response);

                return myIPAddress;
            }
        }
    }
}



My source: http://www.dreamincode.net/code/snippet959.htm[^]

NOTE: The source page has the OLD automation address - use the snippet above which I have modified to use the new one.


string IP = GetIP();

    public string  GetIP()
    {
        string Str = "";
        Str = System.Net.Dns.GetHostName();
        IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(Str);
        IPAddress[] addr = ipEntry.AddressList;
        return addr[addr.Length - 1].ToString();

    }


Get Client''s Real IP Address using C#[^]


这篇关于如何获得用户PC IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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