How to detect static ip using win app in c#

2019-07-31 01:41发布

Please correct me if i am wrong. There are two types of IP - One, the static(fixed) IP address we assign to the LAN card and second that we received from the service provider.

For ex. The IP address set for my machine is 192.168.1.10 while the IP address given by ISP is 218.64.xx.xx. (You can check this using http://www.ip2location.com/)

When I use ASP.net, i can get the IP address provided by ISP using - HttpContext.Current.Request.UserHostAddress;

The Problem: Now, I am working in Windows Forms environment but unable to get the IP provided by ISP, though I am able to get the fixed IP.

Can anybody help me?

Thanks for sharing your time.

5条回答
Rolldiameter
2楼-- · 2019-07-31 02:13

i found manu methods one of them is a html request to http://whatismyip.com

public static IPAddress GetExternalIp()
            {
                string whatIsMyIp = "http://whatismyip.com";
                string getIpRegex = @"(?<=<TITLE>.*)\d*\.\d*\.\d*\.\d*(?=</TITLE>)";
                WebClient wc = new WebClient();
                UTF8Encoding utf8 = new UTF8Encoding();
                string requestHtml = "";
                try
                {
                    requestHtml = utf8.GetString(wc.DownloadData(whatIsMyIp));
                }
                catch (WebException we)
                {
                    // do something with exception
                    Console.Write(we.ToString());
                }
                Regex r = new Regex(getIpRegex);
                Match m = r.Match(requestHtml);
                IPAddress externalIp = null;
                if (m.Success)
                {
                    externalIp = IPAddress.Parse(m.Value);
                }
                return externalIp;
            }

or use

IPHostEntry IPHost = Dns.GetHostEntry(Dns.GetHostName());
Console.Write(IPHost.AddressList[0].ToString());
查看更多
可以哭但决不认输i
3楼-- · 2019-07-31 02:17

You're trying to get the external IP address of your router.

You need to send an HTTP request to a third-party service which will reply with the IP address.

You can do that using the WebClient class.

For example:

///<summary>Gets the computer's external IP address from the internet.</summary>
static IPAddress GetExternalAddress() {
    //<html><head><title>Current IP Check</title></head><body>Current IP Address: 129.98.193.226</body></html>
    var html = new WebClient().DownloadString("http://checkip.dyndns.com/");

    var ipStart = html.IndexOf(": ", StringComparison.OrdinalIgnoreCase) + 2;
    return IPAddress.Parse(html.Substring(ipStart, html.IndexOf("</", ipStart, StringComparison.OrdinalIgnoreCase) - ipStart));
}
查看更多
贪生不怕死
4楼-- · 2019-07-31 02:19

try with this (using System.Net):

IPHostEntry he = Dns.GetHostByName(Dns.GetHostName());
var s = he.AddressList[0].ToString(); // returns IP address
查看更多
一夜七次
5楼-- · 2019-07-31 02:21

The terminology is wrong; your machine has a private IP and a public IP (not "static" and "dynamic").

To get the public IP, you need to bounce off a public server, e.g., whatismyip.org or your own server.

查看更多
女痞
6楼-- · 2019-07-31 02:27

you can try this in the System.Net namespace:

Dns.GetHostAddresses(Dns.GetHostName())
查看更多
登录 后发表回答