现在很多系统,都要在登录时候,确定当前用户所在的位置。这里记录一个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. HD2144Calculate S(n)

    Problem Description Calculate S(n). S(n)=13+23 +33 +......+n3 . Input Each line will contain one int ...

  2. js实现异步循环

    @(编程) 问题 实现异步循环时,你可能会遇到问题. 让我们试着写一个异步方法,每秒打印一次循环的索引值. for(var i = 0; i < 5; i++) { setTimeout(fun ...

  3. hdu 2899 Strange fuction

    http://acm.hdu.edu.cn/showproblem.php?pid=2899 Strange fuction Time Limit: 2000/1000 MS (Java/Others ...

  4. KMP(http://acm.hdu.edu.cn/showproblem.php?pid=1711)

    http://acm.hdu.edu.cn/showproblem.php?pid=1711 #include<stdio.h> #include<math.h> #inclu ...

  5. AutoCAD.NET 不使用P/Invoke方式调用acad.exe或accore.dll中的接口(如acedCommand、acedPostCommand等)

    使用C#进行AutoCAD二次开发,有时候由于C#接口不够完善,或者低版本AutoCAD中的接口缺少,有些工作不能直接通过C#接口来实现,所以需要通过P/Invoke的方式调用AutoCAD的其他DL ...

  6. CISCO ASA 防火墙 IOS恢复与升级

    在IOS被误清除时的处理办法: 1.从tftp上的ios启动防火墙 防火墙启动后 ,按“ESC”键进入监控模式 rommon #2> ADDRESS=192.168.1.116 rommon # ...

  7. Leetcode237:Delete Node in a Linked List

    Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...

  8. ACM之数学题

    数学题,始终记得,第一次被带飞师大校赛以及省赛,毫无例外的在数学题上卡死....因此,现在开始,有意识的保留遇见的数学题...(下列知识点按遇见先后顺序排列: 1欧拉公式 欧拉公式的用处是,找出小于N ...

  9. HDU1000

    哈哈A+B #include<stdio.h> int main() { int a,b; while(scanf("%d%d",&a,&b)!=EOF ...

  10. c++,windows中的字符问题

    string与char*的转换方法 string a; char *b=a.c_str(); string a=new String(b); a=b; LPCWSTR是unicode的字符串,LPCS ...