IPLocationTool.cs 870 B

12345678910111213141516171819202122232425
  1. using Azylee.YeahWeb.HttpUtils;
  2. using Newtonsoft.Json;
  3. using System;
  4. using System.Net;
  5. namespace Azylee.YeahWeb.BaiDuWebAPI.IPLocationAPI
  6. {
  7. public static class IPLocationTool
  8. {
  9. const string URL = "http://map.baidu.com/?qt=ipLocation";
  10. public static IPLocationModel GetLocation()
  11. {
  12. IPLocationWebModel model = null;
  13. try
  14. {
  15. CookieCollection cookie = new CookieCollection();
  16. string rss1 = HttpToolPlus.Get(URL, ref cookie);//第一次请求以获取Cookie
  17. string rss2 = HttpToolPlus.Get(URL, ref cookie);//携带第一次的Cookie获取数据
  18. model = JsonConvert.DeserializeObject<IPLocationWebModel>(rss2);
  19. return model.ToIPLocationModel();
  20. }
  21. catch (Exception e) { return null; }
  22. }
  23. }
  24. }