IPLocationTool.cs 942 B

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