zoukankan      html  css  js  c++  java
  • C#根据用户IP地址查询用户信息

    调用接口来自sina,显示的信息较为全面。
    接口地址为  http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip={ip}
    返回json,格式为如下
    {"ret":1,"start":"59.174.77.0","end":"59.174.80.156","country":"\u4e2d\u56fd",
    "province":"\u6e56\u5317","city":"\u6b66\u6c49","district":"","isp":"\u7535\u4fe1","type":"","desc":""}

     为了方便使用,加入类IpDetai解析json。
    使用方式(ASP.NET):
    Label1.Text = IpHelper.Get("xx.xx.xx.xxx",null).Province;
    另外JSON解析用到了.net 3.5的System.Web.Script.Serialization.JavaScriptSerializer 

    代码片段(2)


    View Code
    using System;

    03 using System.IO;

    04 using System.Net;

    05 using System.Text;

    06 using System.Web.Script.Serialization;

    07

    08 namespace IpUtils

    09 {

    10 public class IpDetail

    11 {

    12 public String Ret { get; set; }

    13

    14 public String Start { get; set; }

    15

    16 public String End { get; set; }

    17

    18 public String Country { get; set; }

    19

    20 public String Province { get; set; }

    21

    22 public String City { get; set; }

    23

    24 public String District { get; set; }

    25

    26 public String Isp { get; set; }

    27

    28 public String Type { get; set; }

    29

    30 public String Desc { get; set; }

    31 }

    32

    33 public class IpHelper

    34 {

    35 /// <summary>

    36 /// 获取IP地址的详细信息,调用的借口为

    37 /// http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip={ip}

    38 /// </summary>

    39 /// <param name="ipAddress">请求分析得IP地址</param>

    40 /// <param name="sourceEncoding">服务器返回的编码类型</param>

    41 /// <returns>IpUtils.IpDetail</returns>

    42 public static IpDetail Get(String ipAddress,Encoding sourceEncoding)

    43 {

    44 String ip = string.Empty;

    45 if(sourceEncoding==null)

    46 sourceEncoding = Encoding.UTF8;

    47 using (var receiveStream = WebRequest.Create("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip="+ipAddress).GetResponse().GetResponseStream())

    48 {

    49 using (var sr = new StreamReader(receiveStream, sourceEncoding))

    50 {

    51 var readbuffer = new char[256];

    52 int n = sr.Read(readbuffer, 0, readbuffer.Length);

    53 int realLen = 0;

    54 while (n > 0)

    55 {

    56 realLen = n;

    57 n = sr.Read(readbuffer, 0, readbuffer.Length);

    58 }

    59 ip = sourceEncoding.GetString(sourceEncoding.GetBytes(readbuffer, 0, realLen));

    60 }

    61 }

    62 return !string.IsNullOrEmpty(ip)?new JavaScriptSerializer().Deserialize<IpDetail>(ip):null;

    63 }

    64 }

    65

    66 public class EncodingHelper

    67 {

    68 public static String GetString(Encoding source, Encoding dest, String soureStr)

    69 {

    70 return dest.GetString(Encoding.Convert(source, dest, source.GetBytes(soureStr)));

    71 }

    72

    73 public static String GetString(Encoding source, Encoding dest, Char[] soureCharArr, int offset, int len)

    74 {

    75 return dest.GetString(Encoding.Convert(source, dest, source.GetBytes(soureCharArr, offset, len)));

    76 }

    77 }

    78 }
  • 相关阅读:
    【转】Java抽象类与接口的区别
    【转】java方法参数传递方式--按值传递、引用传递
    关联mysql失败_Server returns invalid timezone. Go to 'Advanced' tab and set 'serverTimezon'
    分词器的安装与使用
    Mysql、ES 数据同步
    ES、kibana安装及交互操作
    tl-wr742n 怎么设置dns
    tl-wr742n无线路由器怎么设置
    PowerMock学习(十一)之Mock private methods的使用
    PowerMock学习(十)之Mock spy的使用
  • 原文地址:https://www.cnblogs.com/honghuan/p/1970597.html
Copyright © 2011-2022 走看看