c#百度IP定位API使用方法

1.先建立一个收集信息的实体类

IPModel.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Model
{
public class IpModel
{
[Serializable]
public class AddressForQueryIPFromBaidu { public string Address { get; set; } public Content Content { get; set; } public string Status { get; set; } }
[Serializable]
public class Content { public string Address { get; set; } public Address_Detail Address_Detail { get; set; } public Point Point { get; set; } }
[Serializable]
public class Address_Detail { public string City { get; set; } public string City_Code { get; set; } public string District { get; set; } public string Province { get; set; } public string Street { get; set; } public string Street_Number { get; set; } }
[Serializable]
public class Point { public string X { get; set; } public string Y { get; set; } }
}
}

2.获取客户端IP:

string ipAddress = System.Web.HttpContext.Current.Request.UserHostAddress;

3.将获得的IP放入以下函数中

   Model.IpModel.AddressForQueryIPFromBaidu baidu = GetAddressFromIP(ipAddress);
baidu.Content.Address_Detail ;//获取地址
GetAddressFromIP函数:
public static Model.IpModel.AddressForQueryIPFromBaidu GetAddressFromIP(string ip)
{
string baiduKey = "MXAEmre2rCTIYKSAMv1EVh0K";
string url = "http://api.map.baidu.com/location/ip?ak=" + baiduKey +"&ip="+ip+ "&coor=bd09ll";
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
System.IO.Stream responseStream = response.GetResponseStream();
System.IO.StreamReader sr = new System.IO.StreamReader(responseStream, System.Text.Encoding.GetEncoding("utf-8"));
string responseText = sr.ReadToEnd(); sr.Close(); sr.Dispose();
responseStream.Close(); string jsonData = responseText;
JavaScriptSerializer jss = new JavaScriptSerializer();
Model.IpModel.AddressForQueryIPFromBaidu addressForQueryIPFromBaidu = jss.Deserialize<Model.IpModel.AddressForQueryIPFromBaidu>(jsonData);
return addressForQueryIPFromBaidu;
}

此函数里使用到了json需引用看上章

c#百度IP定位API使用方法的更多相关文章

  1. C#调用百度高精度IP定位API通过IP获取地址

    API首页:http://lbsyun.baidu.com/index.php?title=webapi/high-acc-ip 1.申请百度账号,创建应用,获取密钥(AK) http://lbsyu ...

  2. 百度地图定位API,精度提高

    我使用百度定位API DEMO上面好像就可以setCoorType("bd09ll");//百度地图坐标. 然后我找了下从其它坐标体系迁移到百度坐标. 问下: 1.那我还能不能在百 ...

  3. iOS 学习笔记五 【2016年百度地图定位详细使用方法】

    具体介和配置绍在此就不详述了,详情请看百度地图API文档, 在这里具体讲解下,新版百度地图的定位与反地理编码的使用: 1.导入头文件 #import <BaiduMapAPI_Map/BMKMa ...

  4. android中百度地图定位的实现方法(仅适用于真机+WIFI联网环境)

    注意:此代码的环境是:真机(系统版本为Android4.2.2)+WIFI联网(才能实现最后的运行结果):使用虚拟机调试会出现各种问题. 第一步:下载SDK和申请Key 到百度的网站http://de ...

  5. 百度ip定位城市接口调用

    http://lbsyun.baidu.com/index.php?title=webapi/ip-api require 'rubygems' require 'json' print ARGV p ...

  6. 百度地图ip定位,不算bug的bug

    做为一个入行不足两年的菜鸟,能在博客园写下第一篇博客,是需要多大的勇气啊.主要还是怕大神们喷啊.其次自己文笔实在太差了. 哈哈~还请各位大神,口下留情啊. 首先说下我的需求:一个需要城市分站的手机站. ...

  7. 使用百度定位Api获取当前用户登录地址

    最近在做一个商城项目,客户想把网站做成类似于美团的效果,切换地区时,内容也跟随变化.这就要首先解决根据用户id获得地址的问题,最终决定使用百度定位(不适用于搭建反向代理的项目) String url ...

  8. 快速获取IP地址的各种方法

    调用百度的IP定位API(首先需要去百度开放平台注册申请key值http://lbsyun.baidu.com/apiconsole/key) http://api.map.baidu.com/loc ...

  9. web端ip定位

    1/新浪定位 <script src="http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js">&l ...

随机推荐

  1. springboot学习笔记:9.springboot+mybatis+通用mapper+多数据源

    本文承接上一篇文章:springboot学习笔记:8. springboot+druid+mysql+mybatis+通用mapper+pagehelper+mybatis-generator+fre ...

  2. [GX/GZOI2019]宝牌一大堆(DP)

    出这种麻将题有意思吗? 乍看很难实则很水,就是麻将式DP,想必大家很熟悉了吧.首先把“国士无双”和“七对子”两种牌型判掉,然后观察牌胡的形式,发现每多一张牌实际上就是把1个面子变成1个杠子,然后可以直 ...

  3. pycharm、Django+node.js、vue搭建web项目

    参考文章:https://www.wandouip.com/t5i35466/  在此感谢 本篇接着上一篇:windows10使用npm安装vue.vue-cli 首先Django项目是搭建好的,就是 ...

  4. idea maven Running C:\Users\Administrator\AppData\Local\Temp\archetype1tmp

    Running C:\Users\Administrator\AppData\Local\Temp\archetype1tmp 在IDEA中通过maven项目管理工具创建javaweb项目的时候一直卡 ...

  5. 2019-2020-1 20199324《Linux内核原理与分析》第七周作业

    第六章 进程的描述和进程的创建 知识点总结 进程的描述 操作系统内核实现操作系统的三大管理功能以及对应的抽象概念: 进程管理(最核心)-- 进程 内存管理 -- 虚拟内存 文件系统 -- 文件 进程是 ...

  6. 吴裕雄--天生自然python学习笔记:python 创建和操作Firebase实时数据库

    Fire base 是专为移动开发提供的后端服务平台, Firebase 数据库与传统数据库不同,它不是用数据表来存储数据, 而是用 Key . Value 的字典型结构来存储数据,所以它不仅 是轻量 ...

  7. Error、Bias、Variance及损失函数理解

    个人感觉理解误差.偏置.方差的一个好的解释,感谢分享:https://baijiahao.baidu.com/s?id=1601092478839269810&wfr=spider&f ...

  8. iOS有关通讯录操作

    一.首先获取用户通讯录授权信息. 在AppDelegate中导入#import <AddressBook/AddressBook.h>框架,在下列方法中获取授权信息. - (BOOL)ap ...

  9. QLIKVIEW基础设置及初步了解

    改变语言环境 开发工具条勾选出来 创建selection box 创建search box 编辑脚本 重加载数据 基本联动思路:table view tableview load FSUPPLIERI ...

  10. Tricks of Android's GUI

    Tricks of Android's GUI */--> Tricks of Android's GUI 1 layoutweight In LinearLayout, the default ...