现在很多系统,都要在登录时候,确定当前用户所在的位置。这里记录一个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. flask中的request对象方法

    'accept_charsets','accept_encodings','accept_languages','accept_mimetypes','access_route','applicati ...

  2. Python基础 条件、循环

    1.条件语句 Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块. if if语句执行有个特点,它是从上往下判断,如果在某个判断上是True,把该判断对应的 ...

  3. HDU 4497 GCD and LCM (数学,质数分解)

    题意:给定G,L,分别是三个数最大公因数和最小公倍数,问你能找出多少对. 析:数学题,当时就想错了,就没找出规律,思路是这样的. 首先G和L有公因数,就是G,所以就可以用L除以G,然后只要找从1-(n ...

  4. Objective-C 学习记录--toches、Motion/Size/Rect/Point/CGFloat/protocol

    - (void)touchesBegan touchesEnd touchesCancelled touchesMoved //代表的是手指在屏幕上的动作,开始 结束 取消 移动 //还有就是代表摇动 ...

  5. ActiveMQ的消息确认问题

    http://riddickbryant.iteye.com/blog/441890 [发送端] session = connection.createSession(Boolean.FALSE,   ...

  6. JedisPool使用原理和源代码

    1,JedisPool的使用 <!-- 连接池的配置信息 --><beanid="jedisConfig"class="redis.clients.je ...

  7. 无责任Windows Azure SDK .NET开发入门篇三[使用Azure AD 管理用户信息]

    三.使用Azure AD管理用户信息 在上一章我们采用OpenID的方案和Azure AD交互进行身份验证,本章节我们继续了解如何在Azure AD中创建用户,列出用户信息,修改用户信息和删除用户信息 ...

  8. [转][IIS]发布网站,提示用户 'IIS APPPOOL\***' 登录失败。

    链接:http://www.cnblogs.com/tianguook/p/3881075.html 用户 'IIS APPPOOL\DefaultAppPool' 登录失败. 我在windows8中 ...

  9. JOIN 相关内容

    1.left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录  2.right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录 3.inner join ...

  10. MFC 构建、消亡 顺序 (一)--单文档 (SDI)

    MFC 构建.消亡 顺序 (一)--单文档 (SDI) by:http://www.cnblogs.com/vranger/ (一)SDI 生成顺序 (二)打开文档-“Open” (三)新建文档-“N ...