C#获取IP归属地【使用抓取网页内容方式】

此方法是通过IP查询归属地网站的GET方式,发送请求IP,并使用正则表达式抓取返回的归属地


首先:引用命名空间:
       using System.Net;
       using System.IO;
       using System.Text.RegularExpressions;


第二:代码


/// <summary> 
      /// 获取IP归属地 
      /// </summary> 
      /// <param name="Ip">IP地址</param> 
      /// <returns></returns> 
      public string GetIpAddRess(string Ip) 
      { 
          WebRequest request = WebRequest.Create("http://www.ip138.com/ips138.asp?ip=" + Ip); 
          WebResponse response = request.GetResponse(); 
          StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("gb2312")); 
          string read = reader.ReadToEnd(); 
          Regex regex = new Regex("<td align=\"center\"><ul class=\"ul1\"><li>本站主数据:(?<title>.*?)</li>"); 
          if (regex.IsMatch(read)) 
          { 
              read = regex.Match(read).Groups["title"].Value; 
               
          } 
          return read; 
 
      }

直接调用这个方法就可以了,函数的形参是IP地址形式的字符串,函数返回的是IP归属地的字符串

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值