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. Linux介绍,基本命令

  2. locate及find查找命令

    在文件系统上查找符合条件的文件:       实现工具:locate,find locate:       依赖于事先构建好的索引库:       系统自动实现(周期性任务):       手动更新数 ...

  3. TPO4-1 Deer Populations of the Puget Sound

    The causes of this population rebound are consequences of other human actions. First, the major pred ...

  4. CString转换成std::string

    unicode的编码格式: CString strCS; std::string strSTD =  CT2A(strCS.GetBuffer()); 其他的编码格式: CString strCS; ...

  5. Qt 使用自带的OpenGL模块开发程序

    QT中使用opengl .pro文件中添加 QT += opengl 1.使用指定版本的OpenGL如下使用opengl4.5调用方法,使用指定版本的接口,必须设备图形显示设备支持对应OpenGL版本 ...

  6. django框架基础-视图系统-长期维护

    ##################   什么是视图?     ####################### 视图: 1,一个视图函数(类),简称视图,是一个简单的Python 函数(类),它接受W ...

  7. linux 下安装及查看java的安装路径

    一.Linux下安装JDK 1.下载文件 从官网下载合适版本如:jdk-8u191-linux-x64.tar.gz 2.安装文件 1.在 /usr/ 目录下创建 java文件夹mkdir /usr/ ...

  8. cs231n spring 2017 lecture4 Introduction to Neural Networks

    1. Backpropagation:沿着computational graph利用链式法则求导.每个神经元有两个输入x.y,一个输出z,好多层这种神经元连接起来,这时候已知∂L/∂z,可以求出∂L/ ...

  9. mysql 优化配置和方面

    MySQL性能优化的参数简介 公司网站访问量越来越大,MySQL自然成为瓶颈,因此最近我一直在研究 MySQL 的优化,第一步自然想到的是 MySQL 系统参数的优化,作为一个访问量很大的网站(日20 ...

  10. 细看Java序列化机制

    概况 在程序中为了能直接以 Java 对象的形式进行保存,然后再重新得到该 Java 对象,这就需要序列化能力.序列化其实可以看成是一种机制,按照一定的格式将 Java 对象的某状态转成介质可接受的形 ...