获取外网IP地址,并得到该地址所在位置;
如:101.249.255.255
对应:西藏自治区-拉萨市-堆龙德庆区
string ipAddress = GetIPAddress();
string location = GetIPLocation(ipAddress);
/// <summary>
/// 获取IP地址
/// </summary>
/// <returns></returns>
public static string GetIPAddress()
{
try
{
//此接口查询速度最快
var html2 = HttpGetPageHtml("http://www.net.cn/static/customercare/yourip.asp", "gbk");
var ip2 = GetIPFromHtml(html2);
if (!String.IsNullOrEmpty(ip2)) return ip2;
return "";
}
catch (System.Exception ex)
{
mzRunLog.RunlogDebug ("获取IP地址错误:" + ex.Message);
return "";
}
}
/// <summary>
/// 根据IP获取我们所要的信息
/// </summary>
/// <param name="strIp"></param>
/// <returns></returns>
public static string GetIPLocation(string strIp)
{
try
{
if (strIp == "")
return "";
string html = HttpGetPageHtml("https://www.ip138.com/iplookup.asp?ip=" + strIp + "&action=2", "gb2312");
string pre = "var ip_result = {\"ASN归属地\":\"";
int pos = html.IndexOf(pre);
html = html.Substring(pos + pre.Length);
html = html.Substring(0, html.IndexOf(' ')).Replace("移动", "").Replace("联通", "").Replace("电信", "");
//string[] res = html.Split(new char[] { '省', '市', ' ' }, StringSplitOptions.RemoveEmptyEntries);
String regex = "(?<province>[^省]+省|.+自治区)(?<city>[^自治州]+自治州|[^市]+市|[^盟]+盟|[^地区]+地区|.+区划)(?<county>[^市]+市|[^县]+县|[^旗]+旗|.+区)?(?<town>[^区]+区|.+镇)?(?<village>.*)";
// 使用正则表达式匹配省、市、区、镇和村
Match match = Regex.Match(html, regex);
if (match.Success)
{
string province = match.Groups["province"].Value;
string city = match.Groups["city"].Value;
string county = match.Groups["county"].Value;
//string town = match.Groups["town"].Value;
//string village = match.Groups["village"].Value;
//Console.WriteLine("省:" + province);
//Console.WriteLine("市:" + city);
//Console.WriteLine("区/县:" + county);
//Console.WriteLine("镇:" + town);
//Console.WriteLine("村/街道:" + village);
return city;
}
return "";
}
catch (System.Exception ex)
{
mzRunLog.RunlogDebug("获取位置信息错误:" + ex.Message);
return "";
}
}
获取网页信息,解析获取网页中IP地址
/// <summary>
/// 获取页面html
/// </summary>
/// <param name="url">请求的地址</param>
/// <param name="encoding">编码方式</param>
/// <returns></returns>
private 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 ex)
{
throw ex;
}
return pageHtml;
}
/// <summary>
/// 从html中通过正则找到ip信息(只支持ipv4地址)
/// </summary>
/// <param name="pageHtml"></param>
/// <returns></returns>
private static string GetIPFromHtml(String pageHtml)
{
//验证ipv4地址
string reg = @"(?:(?:(25[0-5])|(2[0-4]\d)|((1\d{2})|([1-9]?\d)))\.){3}(?:(25[0-5])|(2[0-4]\d)|((1\d{2})|([1-9]?\d)))";
string ip = "";
Match m = Regex.Match(pageHtml, reg);
if (m.Success)
{
ip = m.Value;
}
return ip;
}
参考: