zoukankan      html  css  js  c++  java
  • C# 根据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 115.193.210.0 115.194.201.255 中国 浙江 杭州 电信


                string[] arr = getAreaInfoList(res);

            }


            /// <summary>

            /// Post请求数据

            /// </summary>

            /// <param name="url"></param>

            /// <returns></returns>

            public string GetDataByPost(string url)

            {

                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);

                string s = "anything";

                byte[] requestBytes = System.Text.Encoding.ASCII.GetBytes(s);

                req.Method = "POST";

                req.ContentType = "application/x-www-form-urlencoded";

                req.ContentLength = requestBytes.Length;

                Stream requestStream = req.GetRequestStream();

                requestStream.Write(requestBytes, 0, requestBytes.Length);

                requestStream.Close();


                HttpWebResponse res = (HttpWebResponse)req.GetResponse();

                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 115.193.210.0 115.194.201.255 中国 浙江 杭州 电信

                string[] areaArr = new string[10];

                string[] newAreaArr = new string[2];

                try

                {

                    //取所要的数据,这里只取省市

                    areaArr = ipData.Split(' ');

                    newAreaArr[0] = areaArr[4];//省

                    newAreaArr[1] = areaArr[5];//市

                }

                catch (Exception e)

                {

                    // TODO: handle exception

                }

                return newAreaArr;

            }

  • 相关阅读:
    自定义UILabel,使文字居左上显示
    xcode 7 运行项目报错 -fembed-bitcode is not supported on versions of iOS prior to 6.0
    git 如何删除本地未提交的文件
    coco2d-x技术
    mac 查看端口是否被使用
    ios 提交
    oc基础复习10-OC的id
    oc基础复习09-OC的self 和super(深入理解)
    oc基础复习08-OC的类方法
    oc基础复习07-OC的弱语法(转)
  • 原文地址:https://www.cnblogs.com/hefeilong/p/5714925.html
Copyright © 2011-2022 走看看