PublicIPAddressTool.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using Azylee.Core.DataUtils.StringUtils;
  2. using Azylee.Jsons;
  3. using Azylee.YeahWeb.HttpUtils;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. namespace Azylee.YeahWeb.ExtWebAPI.IPAddressAPI
  9. {
  10. /// <summary>
  11. /// 公网IP地址API
  12. /// </summary>
  13. public class PublicIPAddressTool
  14. {
  15. const string URL = "http://pv.sohu.com/cityjson?ie=utf-8";
  16. const string URL_IP = "https://www.ipplus360.com/getIP";
  17. const string URL_LOC = "https://www.ipplus360.com/getLocation";
  18. /// <summary>
  19. /// 获取公网IP地址API
  20. /// </summary>
  21. /// <returns></returns>
  22. public static PublicIPAddressModel GetPublicIP()
  23. {
  24. string rs = HttpTool.Get(URL);
  25. if (Str.Ok(rs))
  26. {
  27. int flagbeg = rs.IndexOf("{");
  28. int flagend = rs.LastIndexOf("}");
  29. if (flagbeg < flagend)
  30. {
  31. rs = rs.Substring(flagbeg, flagend - flagbeg + 1);
  32. Dictionary<string, string> model = Json.String2Object<Dictionary<string, string>>(rs);
  33. if (model != null)
  34. {
  35. model.TryGetValue("cip", out string cip);
  36. model.TryGetValue("cid", out string cid);
  37. model.TryGetValue("cname", out string cname);
  38. PublicIPAddressModel result = new PublicIPAddressModel(cip, cid, cname);
  39. return result;
  40. }
  41. }
  42. }
  43. return null;
  44. }
  45. public static string GetIp()
  46. {
  47. string ip = "";
  48. try
  49. {
  50. Dictionary<string, string> keyValuePairs = HttpTool.Get<Dictionary<string, string>>(URL_IP);
  51. }catch(Exception ex) { }
  52. return ip;
  53. }
  54. public static string GetLocation()
  55. {
  56. string location = "";
  57. return location;
  58. }
  59. }
  60. }