zoukankan      html  css  js  c++  java
  • 根据IP获取省市

    /// <summary>
    /// 根据IP获取省市
    /// </summary>
    public void GetAddressByIp()
    {
    string ip = "115.193.217.249";
    string PostUrl = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip=" + ip;
    string res = GetDataByPost(PostUrl);//该条请求返回的数据为:res=1\t115.193.210.0\t115.194.201.255\t中国\t浙江\t杭州\t电信

    string[] arr = getAreaInfoList(res);
    }

    /// <summary>
    /// Post请求数据
    /// </summary>
    /// <param name="url"></param>
    /// <returns></returns>
    public string GetDataByPost(string url)
    {
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);

    HttpWebResponse res = (HttpWebResponse)req.GetResponse();
    Stream stream = res.GetResponseStream();
    StreamReader sr = new StreamReader(res.GetResponseStream(), System.Text.Encoding.Default);
    string backstr = sr.ReadToEnd();
    sr.Close();
    res.Close();
    return backstr;
    }

    /// <summary>
    /// 处理所要的数据
    /// </summary>
    /// <param name="ip"></param>
    /// <returns></returns>
    public static string[] getAreaInfoList(string ipData)
    {
    //1\t115.193.210.0\t115.194.201.255\t中国\t浙江\t杭州\t电信
    string[] areaArr = new string[10];
    string[] newAreaArr = new string[2];
    try
    {
    //取所要的数据,这里只取省市
    areaArr = ipData.Split('\t');
    newAreaArr[0] = areaArr[4];//省
    newAreaArr[1] = areaArr[5];//市
    }
    catch (Exception e)
    {
    // TODO: handle exception
    }
    return newAreaArr;
    }

  • 相关阅读:
    notes: the architecture of GDB
    How systems researchers build systems
    spark1.1.0源码阅读-executor
    spark1.1.0源码阅读-taskScheduler
    spark1.1.0源码阅读-dagscheduler and stage
    akka简单示例-2
    环境安装与项目配置
    安装zsh
    linux mysql 密码修改
    django-debug-toolbar 安装及配置 django性能监控及调试
  • 原文地址:https://www.cnblogs.com/KingsLiu/p/5807804.html
Copyright © 2011-2022 走看看