How to detect static ip using win app in c#

橙三吉。 提交于 2019-12-24 09:36:39

问题


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.


回答1:


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));
}



回答2:


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.




回答3:


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());



回答4:


you can try this in the System.Net namespace:

Dns.GetHostAddresses(Dns.GetHostName())



回答5:


try with this (using System.Net):

IPHostEntry he = Dns.GetHostByName(Dns.GetHostName());
var s = he.AddressList[0].ToString(); // returns IP address


来源:https://stackoverflow.com/questions/4899436/how-to-detect-static-ip-using-win-app-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!