Smart way to get the public Internet IP address/ge

2019-02-07 11:21发布

I have a computer on the local network, behind a NAT router. I have some 192.168.0.x addresses, but I really want to know my public IP address, not something mentioned in

How to get the IP address of the server on which my C# application is running on?

or

How to get the IP address of a machine in C#

I need C# code.

Is it possible? If so, how?

9条回答
霸刀☆藐视天下
2楼-- · 2019-02-07 12:01

Below code will help you to take public IP address

    string _externalIP;
    _externalIP = (new WebClient()).DownloadString("http://http://icanhazip.com/");
    _externalIP = (new Regex(@"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"))
                 .Matches(externalIP)[0].ToString();
查看更多
姐就是有狂的资本
3楼-- · 2019-02-07 12:06

Depending on the router you use, chances are pretty good that you could get it directly from the router. Most of them have a web interface, so it would be a matter of navigating to the correct web page (e.g., "192.168.0.1/whatever") and "scraping" the external IP address from that page. The problem with this is, of course, that it's pretty fragile -- if you change (or even re-configure) your router, chances are pretty good that it'll break.

查看更多
Fickle 薄情
4楼-- · 2019-02-07 12:08

you may be able to use uPNP and fall-back to whatsmyip.com if that fails.

查看更多
登录 后发表回答