【问题标题】:How to get internet IP address from C# Windows App [duplicate]如何从 C# Windows 应用程序获取互联网 IP 地址 [重复]
【发布时间】:2011-06-01 18:31:03
【问题描述】:

可能的重复:
how to get my own IP address in C#?
how to get ip address of machine in c#

大家好,我目前正在使用 WPF 为 windows 开发一个 c# 应用程序。我想获取计算机的外部 IP 地址,即互联网地址,而不是本地计算机 IP 地址或本地路由器地址。

感谢您的帮助。

【问题讨论】:

  • 除非这个问题得到解决,否则我们手头有一式三份……三份……三份……
  • 请注意,这些问题的答案倾向于显示任何 ipv4 ip,而不是互联网地址——这就是我认为不应关闭的原因。
  • @NickAldwin 我同意 OP 想知道互联网地址,而作为重复给出的其他问题可能\可能返回本地 IP 地址。

标签: c# wpf


【解决方案1】:

如前所述,您需要一个外部 Web 服务器。使用 URL“http://checkip.dyndns.org/”轻松调用 HTTP GET 将为您提供一个带有您 IP 的简单文本字符串。

【讨论】:

    【解决方案2】:

    您需要在云中的某个位置有一个 Web 服务器,以便您可以调用它并能够为您提供您的外部 IP 地址。

    看起来this 一个是免费的。

    【讨论】:

      【解决方案3】:

      [编辑] 向here 发送一个简单请求即可获取您的 IP。

      这是一种获取 cmets 中指出的任何网络地址(不一定是互联网 ip)的方法:

      IPAddress host = IPAddress.None;
      foreach (IPAddress ip in Dns.GetHostAddresses(Dns.GetHostName()))
          {
              host = ip;
              if (ip.AddressFamily == AddressFamily.InterNetwork)
                   break;
          }
      

      【讨论】:

      • 这将检索本地机器的第一个 IPv4 地址,不一定是机器的外部地址(甚至不能保证是可访问 Internet 的接口)。
      • 您的编辑回答了这个问题。删除旧代码,你会得到我的支持。 :)
      • @chibacity 我把它留在里面是为了让 Simon 的评论看起来不奇怪......并澄清一下。
      【解决方案4】:

      我发现的唯一方法是对http://www.whatismyip.com/automation/n09230945.asp 执行 httpWebRequest 并解析 ip 的结果

      【讨论】:

      • 他们特别希望你不要那样做。见网源评论:<!--Please set your code to scrape your IP from www.whatismyip.com/automation/n09230945.asp For more info, please see our "Recommended Automation Practices" thread in the Forum.-->
      【解决方案5】:

      您可以尝试连接whatismyip.com,如下代码所示:

      using System;
      using System.Net;
      using System.Text;
      using System.Text.RegularExpressions;
      
      namespace DreamInCode.Snippets
      {
          public class IpFinder
          {
              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;
              }
          }
      }
      

      注意:代码来自这篇帖子http://www.dreamincode.net/forums/topic/24692-showing-the-external-ip-address-in-c%23/

      【讨论】:

      • 他们特别希望你不要那样做。请参阅网络源中的评论:&lt;!--Please set your code to scrape your IP from www.whatismyip.com/automation/n09230945.asp For more info, please see our "Recommended Automation Practices" thread in the Forum.--&gt; 不需要抓取 - 因为它只是 ip 的纯文本!他们甚至还在标题中包含 IP 吗?
      猜你喜欢
      • 2018-10-11
      • 1970-01-01
      • 2011-08-01
      • 2013-08-06
      • 2015-04-17
      • 2015-01-23
      • 1970-01-01
      • 2013-06-27
      • 2011-11-12
      相关资源
      最近更新 更多