现在很多系统,都要在登录时候,确定当前用户所在的位置。这里记录一个C#使用Http的方式获取当前IP所在的位置信息。主要使用的api是新浪的接口。

 public partial class sina : System.Web.UI.Page
{
protected string ip = "171.216.18.89";
protected void Page_Load(object sender, EventArgs e)
{
//if (IPOperation.GetIP() != "127.0.0.1")
//{
// ip = IPOperation.GetIP();
//}
this.ClientScript.RegisterStartupScript(this.GetType(), "message", "<script language='javascript' defer>alert('" + GetCityByIP(ip).ToString() + "');</script>"); }
private string GetCityByIP(string ip)
{
string cityName = "成都";
if (!string.IsNullOrEmpty(ip))
{
string url = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=" + ip;
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "POST";
request.ContentType = "application/json;charset=utf-8";
Stream stream = request.GetResponse().GetResponseStream();
StreamReader sr = new StreamReader(stream, Encoding.UTF8);
string str = sr.ReadToEnd().Split('=')[].ToString().TrimEnd(';'); IPArea list = new JavaScriptSerializer().Deserialize<IPArea>(str);
cityName = list.city;
}
return cityName;
}
}
class IPArea
{
public int ret { get; set; }
public string start { get; set; }
public string end { get; set; }
public string country { get; set; }
public string province { get; set; }
public string city { get; set; }
public string district { get; set; }
public string isp { get; set; }
public string type { get; set; }
public string desc { get; set; }
}

  主要先创建一个HttpWebRequest,定义请求方式及返回数据的结构和编码。这里使用JavaScriptSerializer将返回的json对象反序列化为对象。关于对象的信息就不记载了。

根据当前IP获取当时所在信息的更多相关文章

  1. JS 利用新浪接口通过IP地址获取当前所在城市

    <html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">< ...

  2. 根据ip获取用户地理位置

    各大网站都提供根据ip获取用户地理位置信息,这里以新浪的接口为例子 接口地址为:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js ...

  3. 百度地图api根据用户IP获取用户位置(PHP)

    1.百度地图开放平台找的你的ak ,链接:http://lbsyun.baidu.com/apiconsole/key 2.获取用户ip地址(外网ip 服务器上可以获取用户外网Ip 本机ip地址只能获 ...

  4. ip获取所在城市名称等信息接口,及函数

    函数: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 ...

  5. C# 解析百度天气数据,Rss解析百度新闻以及根据IP获取所在城市

    百度天气 接口地址:http://api.map.baidu.com/telematics/v3/weather?location=上海&output=json&ak=hXWAgbsC ...

  6. php根据IP获取经纬度信息--百度地图篇

    一.前言 之前一篇写过 php根据IP获取IP所在城市  ,但是还想再精确一点,获取这个IP所在的经纬度信息,该怎么办呢? 百度地图为我提供了一种解决方案(当然还有其他的解决方案). 先总的来数一下, ...

  7. 获取ip地址及城市信息

    大家好,今天给大家分享的是一个简单的知识获取登录用户的ip地址及城市信息,lz是一个小白,如果有哪些错误的地方  欢迎大家指出 东西很简单,直接上代码 [HttpPost] public string ...

  8. 根据IP定位用户所在城市信息

    http://www.9958.pw/post/city_ip 1.调用新浪IP地址库 新浪提供了开放的IP地址库数据供开发者调用,调用地址: http://int.dpool.sina.com.cn ...

  9. 根据IP地址获取地址所在城市帮助类(IPHelper)

    很多类库都是需要在长时间的编写过程中进行积累的,进入软件编程行业已经是第五个年头了,从2011年写下第一行代码到现在不知道已经写了多少行代码了,时间也过得挺快的.最近事情比较多,也很少写博客了,最近项 ...

随机推荐

  1. Mysql SQL优化&执行计划

    SQL优化准则 禁用select * 使用select count(*) 统计行数 尽量少运算 尽量避免全表扫描,如果可以,在过滤列建立索引 尽量避免在where子句对字段进行null判断 尽量避免在 ...

  2. USB -- BULK_ONLY和UFI协议

    2 BULK_ONLY和UFI协议 Bulk—Only协议是USB组织针对大容量存储设备制定的一种块存储类协议,目前已经普遍应用于各种移动存储设备. USB设备分为5大类,即显示器.通信设备.音频设备 ...

  3. UVa11997K Smallest Sums(优先队列)

    K Smallest Sums You're given k arrays, each array has k integers. There are kk ways to pick exactly ...

  4. VS2008注册码

    PYHYP-WXB3B-B2CCM-V9DX9-VDY8T 如果下载的是90天的试用版,下载下来以后把ISO里面的Setup\setup.sdb文件用记事本打开,把其中的[Product Key]下面 ...

  5. php redis 分布式类

    配置: $redis_config = array( 'prefix' => 'ylmall_', 'master' => array( 'host' => "192.16 ...

  6. jpa动态创建EntityManagerFactory 态设置数据库连接 EntityManager;

    //jpa动态创建EntityManagerFactory 态设置数据库连接EntityManager;createEntityManagerFactory(String persistenceUni ...

  7. 优化checkbox和radio,类似Bootstrap中的iCheck

    checkbox和radio浏览器默认的已经满足不了大众的审美需求,更不用说浏览器之间的差异化,取而代之,优化checkbox和radio的方法也随之诞生了. html结构:单选框为例,简单说明: 其 ...

  8. android 动画属性(一)之Animation

    Animation 在android 程序当中很多时候要用到动画效果,而动画效果主要是Animation来实现的,API给出的解释: 其中包含4种动画效果 AlphaAnimation 渐变透明度 R ...

  9. 索引的实现:B+树

    [ http://blog.csdn.net/stormbjm/article/details/12033673 ]   见<数据库系统概念>P323.   [从B树.B+树.B*树谈到R ...

  10. non-manifold Mesh(非流形网格)

    三角网格曲面中,大多的算法基于流形网格manifold mesh,其定义如下: 1)Each edge is incident to only one or two faces: 一条网格边为一个或两 ...