根据当前IP获取当时所在信息
现在很多系统,都要在登录时候,确定当前用户所在的位置。这里记录一个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获取当时所在信息的更多相关文章
- JS 利用新浪接口通过IP地址获取当前所在城市
<html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">< ...
- 根据ip获取用户地理位置
各大网站都提供根据ip获取用户地理位置信息,这里以新浪的接口为例子 接口地址为:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js ...
- 百度地图api根据用户IP获取用户位置(PHP)
1.百度地图开放平台找的你的ak ,链接:http://lbsyun.baidu.com/apiconsole/key 2.获取用户ip地址(外网ip 服务器上可以获取用户外网Ip 本机ip地址只能获 ...
- 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 ...
- C# 解析百度天气数据,Rss解析百度新闻以及根据IP获取所在城市
百度天气 接口地址:http://api.map.baidu.com/telematics/v3/weather?location=上海&output=json&ak=hXWAgbsC ...
- php根据IP获取经纬度信息--百度地图篇
一.前言 之前一篇写过 php根据IP获取IP所在城市 ,但是还想再精确一点,获取这个IP所在的经纬度信息,该怎么办呢? 百度地图为我提供了一种解决方案(当然还有其他的解决方案). 先总的来数一下, ...
- 获取ip地址及城市信息
大家好,今天给大家分享的是一个简单的知识获取登录用户的ip地址及城市信息,lz是一个小白,如果有哪些错误的地方 欢迎大家指出 东西很简单,直接上代码 [HttpPost] public string ...
- 根据IP定位用户所在城市信息
http://www.9958.pw/post/city_ip 1.调用新浪IP地址库 新浪提供了开放的IP地址库数据供开发者调用,调用地址: http://int.dpool.sina.com.cn ...
- 根据IP地址获取地址所在城市帮助类(IPHelper)
很多类库都是需要在长时间的编写过程中进行积累的,进入软件编程行业已经是第五个年头了,从2011年写下第一行代码到现在不知道已经写了多少行代码了,时间也过得挺快的.最近事情比较多,也很少写博客了,最近项 ...
随机推荐
- 门户级UGC系统的技术进化路线——新浪新闻评论系统的架构演进和经验总结(转)
add by zhj:先收藏了 摘要:评论系统是所有门户网站的核心标准服务组件之一.本文作者曾负责新浪网评论系统多年,这套系统不仅服务于门户新闻业务,还包括调查.投票等产品,经历了从单机到多机再到集群 ...
- 【多线程】Java并发编程:Lock(转载)
原文链接:http://www.cnblogs.com/dolphin0520/p/3923167.html Java并发编程:Lock 在上一篇文章中我们讲到了如何使用关键字synchronized ...
- 数据结构(C语言版)---第三章栈和队列 3.4.2 队列的链式表示和实现(循环队列)
这个是循环队列的实现,至于串及数组这两章,等有空再看,下面将学习树. 源码如下: #include <stdio.h> #include <stdlib.h> #define ...
- UVALive 7455 Linear Ecosystem (高斯消元)
Linear Ecosystem 题目链接: http://acm.hust.edu.cn/vjudge/contest/127401#problem/B Description http://7xj ...
- excel分组求和
=SUMPRODUCT((C2:C99=F2)*(B2:B99)) 说明: C2:C99=F2 找到C2到C99之间的等于F2的值 如果有多个判断条件,采用*来管理 B2:B99 求和
- USB枚举过程(2)
用bus hound 得到的数据 GET MAX LUN 命令 详见USB_MSC_BlukOnly_v1.0 接下来用到的是UFI SCSI
- LightOJ 1370 - Bi-shoe and Phi-shoe (欧拉函数思想)
http://lightoj.com/volume_showproblem.php?problem=1370 Bi-shoe and Phi-shoe Time Limit:2000MS Me ...
- windows下php cli模式,提示出错
搞一下php cli,一直提示,找不到php_pdo_mssql.dll模块 原来是在php.ini加载了该模块,但ext下又没有该模块 即便下载了一个php_pdo_mssql.dll,但还是会提示 ...
- jdbc调用存储过程的方法
----------------------------jdbc调用存储过程的方法---------------------------------------------------private ...
- 使用virtualenvwrapper隔离python环境
今天使用的是virtualenvwrapper,相较于使用virtualenv, 好处就是把所有环境都放在同一目录下管理,以便更好的管理及切换. 首先需要确认有 pip 是否已经安装, 若没有需要先安 ...