根据IP地址获取地址所在城市帮助类(IPHelper)
2015-03-20 22:10
302 查看
很多类库都是需要在长时间的编写过程中进行积累的,进入软件编程行业已经是第五个年头了,从2011年写下第一行代码到现在不知道已经写了多少行代码了,时间也过得挺快的。最近事情比较多,也很少写博客了,最近项目中需要匹配所有用户的IP登录城市信息,所以需要写一个方法来匹配,第一个反映就是IP地址库,但是这个地址库肯定不是很全面,必须要通过其他的API调用来实现此功能。
思路构建
1.先通过本地的测IP地址库进行匹配
2.如果本地IP地址库存在此IP的城市信息,就直接返回,调用速度也快
3.如果本地没有对应的IP城市信息,必须通过调用网络的IP查询的API了,这里我使用了(百度,新浪,和淘宝)
注意:百度的调用不是很正常,大批量调用
开始编写代码:
程序调用
参数分别是调用api类别值,和IP地址
完成此功能对于使用客户的区域密集度有很好的分析,数据采集回来之后需要对数据进行准确有用的分析,从获取有用的信息,来对程序的使用人群进行分析了。
编写时间:2015年3月20日22:09:25
思路构建
1.先通过本地的测IP地址库进行匹配
2.如果本地IP地址库存在此IP的城市信息,就直接返回,调用速度也快
3.如果本地没有对应的IP城市信息,必须通过调用网络的IP查询的API了,这里我使用了(百度,新浪,和淘宝)
注意:百度的调用不是很正常,大批量调用
开始编写代码:
#region 调用百度 新浪和淘宝接口返回地址 1.百度 2.新浪 3.淘宝 /// <summary> /// 调用百度 新浪和淘宝接口返回地址 1.百度 2.新浪 3.淘宝 /// </summary> /// <param name="Serve">1.百度 2.新浪 3.淘宝</param> /// <param name="ipAddress"></param> /// <returns></returns> public string GetAddress(int Serve, string ipAddress) { try { var match = new Regex(@"((?:(?: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))))"); if (!match.IsMatch(ipAddress)) { return string.Empty; } var webClient = new System.Net.WebClient(); NameValueCollection postValues = null; // 向服务器发送POST数据 var url = string.Empty; if (Serve == 1) { url = "http://api.map.baidu.com/location/ip"; postValues = new System.Collections.Specialized.NameValueCollection { {"ak", "MRkBd6jnGOf8O5F58KKrvit5"}, {"ip", ipAddress}, {"coor", "bd09ll"} }; } else if (Serve == 2) { url = "http://int.dpool.sina.com.cn/iplookup/iplookup.php"; postValues = new System.Collections.Specialized.NameValueCollection { {"format", "json"}, {"ip", ipAddress} }; } else { url = "http://ip.taobao.com/service/getIpInfo.php"; postValues = new System.Collections.Specialized.NameValueCollection { {"ip", ipAddress} }; } byte[] responseArray = webClient.UploadValues(url, postValues); string response = System.Text.Encoding.UTF8.GetString(responseArray); var dataJson = JObject.Parse(response); //动态解析 正常的解析无法生效 string address = string.Empty; //百度接口 if (Serve == 1) { if (dataJson["status"].ToString() == "0") { address = dataJson["content"]["address_detail"]["province"] + "," + dataJson["content"]["address_detail"]["city"]; } } //新浪接口 else if (Serve == 2) { if (dataJson["ret"].ToString() == "1") { address = dataJson["province"] + "," + dataJson["city"]; } } //淘宝接口 else { if (dataJson["code"].ToString() == "0") { if (!string.IsNullOrEmpty(dataJson["data"]["region"].ToString())) address = dataJson["data"]["region"] + "," + dataJson["data"]["city"]; } } if (string.IsNullOrEmpty(address)) { address = "局域网"; } return address; } catch(Exception ex) { Console.WriteLine(ex.Message); return string.Empty; } } #endregion
程序调用
参数分别是调用api类别值,和IP地址
ipAddressName = ipHelper.GetAddress(2, ipaddress);//调用新浪接口返回数据
完成此功能对于使用客户的区域密集度有很好的分析,数据采集回来之后需要对数据进行准确有用的分析,从获取有用的信息,来对程序的使用人群进行分析了。
编写时间:2015年3月20日22:09:25
相关文章推荐
- 根据IP地址获取地址所在城市帮助类(IPHelper)
- 根据ip地址获取所在省市城市地区的真实地址/php函数
- PHP 根据IP地址获取所在城市
- 新浪接口之PHP根据IP地址获取所在城市
- PHP 根据IP地址获取所在城市
- PHP:根据IP地址获取所在城市
- 根据IP地址获取所在城市
- PHP根据IP地址获取所在城市具体实现
- PHP 根据IP地址获取所在城市
- PHP 根据IP地址获取所在城市
- 根据IP地址获取所在城市
- 根据IP地址获取所在城市【通过新浪接口】
- 根据IP地址获取所在城市
- PHP 根据IP地址获取所在城市
- 根据IP地址获取所在城市【通过新浪接口】
- js根据ip地址获取所在城市
- PHP根据IP地址获取所在城市具体实现
- PHP 根据IP地址获取所在城市
- PHP根据IP地址获取所在城市具体实现
- PHP根据IP地址获取所在城市