C# 根據IP地址擷取城市

來源:互聯網
上載者:User

標籤:des   style   class   blog   code   http   

 1 using System; 2 using System.IO; 3 using System.Net; 4 using System.Text; 5 using System.Web.Script.Serialization; 6  7 namespace IpUtils 8 { 9     public class IpDetail10     {11         public String Ret { get; set; }12 13         public String Start { get; set; }14 15         public String End { get; set; }16 17         public String Country { get; set; }18 19         public String Province { get; set; }20 21         public String City { get; set; }22 23         public String District { get; set; }24 25         public String Isp { get; set; }26 27         public String Type { get; set; }28 29         public String Desc { get; set; }30     }31 32     public class IpHelper33     {34         /// <summary>35         /// 擷取IP地址的詳細資料,調用的借口為36         /// http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip={ip}37         /// </summary>38         /// <param name="ipAddress">請求分析得IP地址</param>39         /// <param name="sourceEncoding">伺服器返回的編碼類別型</param>40         /// <returns>IpUtils.IpDetail</returns>41         public static IpDetail Get(String ipAddress,Encoding sourceEncoding)42         {43             String ip = string.Empty;44             if(sourceEncoding==null)45                 sourceEncoding = Encoding.UTF8;46             using (var receiveStream = WebRequest.Create("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip="+ipAddress).GetResponse().GetResponseStream())47             {48                 using (var sr = new StreamReader(receiveStream, sourceEncoding))49                 {50                     var readbuffer = new char[256];51                     int n = sr.Read(readbuffer, 0, readbuffer.Length);52                     int realLen = 0;53                     while (n > 0)54                     {55                         realLen = n;56                         n = sr.Read(readbuffer, 0, readbuffer.Length);57                     }58                     ip = sourceEncoding.GetString(sourceEncoding.GetBytes(readbuffer, 0, realLen));59                 }60             }61             return  !string.IsNullOrEmpty(ip)?new JavaScriptSerializer().Deserialize<IpDetail>(ip):null;62         }63     }64 65     public class EncodingHelper66     {67         public static String GetString(Encoding source, Encoding dest, String soureStr)68         {69             return dest.GetString(Encoding.Convert(source, dest, source.GetBytes(soureStr)));70         }71 72         public static String GetString(Encoding source, Encoding dest, Char[] soureCharArr, int offset, int len)73         {74             return dest.GetString(Encoding.Convert(source, dest, source.GetBytes(soureCharArr, offset, len)));75         }76     }77 }
IpHelper

 

.NET技術交流群 199281001 .歡迎加入。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.