Asp.net根据IP显示省市拼音源码
2015-11-13 14:30
239 查看
根据IP地址显示城市拼音
可以获取客户端的IP地址,浏览者的所在城市,城市的全拼,二级域名。
其数据来源于mdb数据库,里面包含两个表:city_py和ip_address,其中city_py可以应用于根据拼音首字母检索城市。
关键代码:
运行效果图:
演示代码下载地址:
可以获取客户端的IP地址,浏览者的所在城市,城市的全拼,二级域名。
其数据来源于mdb数据库,里面包含两个表:city_py和ip_address,其中city_py可以应用于根据拼音首字母检索城市。
关键代码:
// 获取客户端所在城市,省份,跳转域名 public string[] GetCustomCity() { string Ip = GetClientIP(); string[] ipcontent = new string[3]; try { long ipint = IPtoNum(Ip); OleDbConnection oleconn = new OleDbConnection(); oleconn.ConnectionString = CONNSTRING; OleDbCommand OleCommand = new OleDbCommand(); OleCommand.Connection = oleconn; OleCommand.CommandText = COMMANDTEXT + ipint.ToString() + ">=ip1 and " + ipint.ToString() + "<=ip2 order by id desc"; oleconn.Open(); OleDbDataReader reader = OleCommand.ExecuteReader(); if (reader.HasRows) { reader.Read(); ipcontent[0] = reader["city"].ToString(); ipcontent[1] = reader["province"].ToString(); } else { ipcontent[0] = ""; ipcontent[1] = ""; } oleconn.Close(); OleCommand.CommandText = selectcityname + "'" + ipcontent[0].ToString() + "'"; oleconn.Open(); reader = OleCommand.ExecuteReader(); if (reader.Read()) { ipcontent[2] = reader["pinyin"].ToString(); } else { ipcontent[2] = ":该客户端的IP地址信息在数据库中没有找到相关信息"; } } catch (Exception ex) { ipcontent[0] = ex.Message.ToString(); ipcontent[1] = ex.Source.ToString(); ipcontent[2] = ex.ToString(); } return ipcontent; } //将IP 地址转化为数字 public long IPtoNum(string Ip) { string[] stringip = new string[4]; stringip = Ip.Split('.'); long ipnum = Convert.ToInt64((stringip[0])) * 16777216 + Convert.ToInt64(stringip[1]) * 65536 + Convert.ToInt64(stringip[2]) * 256 + Convert.ToInt64(stringip[3]); return ipnum; } //获取客户端的ip地址 public string GetClientIP() { string result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; if (null == result || result == String.Empty) { result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; } if (null == result || result == String.Empty) { result = HttpContext.Current.Request.UserHostAddress; } return result; }
运行效果图:
演示代码下载地址:
相关文章推荐
- ASP.NET WebApi 简单记录
- Asp.net中多行TextBox随着输入的函数多少自动改变
- asp.net验证码及怎么获取里面的数值(整合)
- 【Codeforces Round 323 (Div 2)A】【水题】Asphalting Roads 行列之进行首次操作
- IIS5、IIS6、IIS7的ASP.net请求处理过程比较
- asp.net css失效
- asp.net中DateTime使用
- asp.net 下载文件
- 在ASP.NET中实现多文件上传
- Asp.Net中图片大小的缩放
- ASP.NET WebService的图片验证码服务
- ASP.NET调用JAVA的CXF生成的Webservice,并带有Soapheader验证的解决方案
- ASP.NET在IIS7.5(IIS7)配置伪静态
- ExtAspNet2.1.0版本
- IBatis.net在asp.net MVC下的使用
- ASP.NET 抓取网页
- 摘抄——OWASP_Code_Review_Guide-V1_1 (1)
- asp.net 子页面关闭刷新父页面
- asp.net 子页面关闭刷新父页面
- asp.net夜话之五:Page类和回调技术