[C#]获取公网IP的三种方法

2022-06-13 18:27:23 浏览数 (1)

代码语言:javascript复制
void Main()
{
  var t0_html = HttpGetPageHtml("https://tool.lu/ip/", "utf-8");
  var t1_html = HttpGetPageHtml("http://www.ip111.cn/",  "utf-8");
  var t2_html = HttpGetPageHtml("http://www.882667.com/",  "utf-8");
  var t0_ip = GetIPFromHtml(t0_html).Dump("https://tool.lu/ip/");// 
  var t1_ip = GetIPFromHtml(t1_html).Dump("http://www.ip111.cn/");;// 
  var t2_ip = GetIPFromHtml(t2_html).Dump("http://www.882667.com/");;// 

}

/// <summary>
/// 获取页面html
/// </summary>
/// <param name="url">请求的地址</param>
/// <param name="encoding">编码方式</param>
/// <returns></returns>
public static string HttpGetPageHtml(string url, string encoding)
{
  string pageHtml = string.Empty;
  try
  {
    using (WebClient MyWebClient = new WebClient())
    {
      Encoding encode = Encoding.GetEncoding(encoding);
      MyWebClient.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.84 Safari/537.36");
      MyWebClient.Credentials = CredentialCache.DefaultCredentials;//获取或设置用于向Internet资源的请求进行身份验证的网络凭据
      Byte[] pageData = MyWebClient.DownloadData(url); //从指定网站下载数据
      pageHtml = encode.GetString(pageData);
    }
  }
  catch (Exception e)
  {

  }
  return pageHtml;
}
/// <summary>
/// 从html中通过正则找到ip信息(只支持ipv4地址)
/// </summary>
/// <param name="pageHtml"></param>
/// <returns></returns>
public static string GetIPFromHtml(String pageHtml)
{
  //验证ipv4地址
  string reg = @"(?:(?:(25[0-5])|(2[0-4]d)|((1d{2})|([1-9]?d))).){3}(?:(25[0-5])|(2[0-4]d)|((1d{2})|([1-9]?d)))";
  string ip = "";
  Match m = Regex.Match(pageHtml, reg);
  if (m.Success)
  {
    ip = m.Value;
  }
  return ip;
}

c# ip

0 人点赞