Просмотр исходного кода

!965 fix: 获取外网IP地址
Merge pull request !965 from 高级CV工程师/next

zuohuaijun 2 лет назад
Родитель
Сommit
087ccb6d33
1 измененных файлов с 11 добавлено и 5 удалено
  1. 11 5
      Admin.NET/Admin.NET.Core/Util/ComputerUtil.cs

+ 11 - 5
Admin.NET/Admin.NET.Core/Util/ComputerUtil.cs

@@ -151,11 +151,10 @@ public static class ComputerUtil
     /// <returns></returns>
     public static string GetIpFromOnline()
     {
-        var url = "http://myip.ipip.net";
-        var stream = url.GetAsStreamAsync().GetAwaiter().GetResult();
-        var streamReader = new StreamReader(stream.Stream, stream.Encoding);
-        var html = streamReader.ReadToEnd();
-        return !html.Contains("当前 IP:") ? "未知" : html.Replace("当前 IP:", "").Replace("来自于:", "");
+        var url = "https://www.ip.cn/api/index?ip&type=0";
+        var str = url.GetAsStringAsync().GetAwaiter().GetResult();
+        var resp = JSON.Deserialize<IpCnResp>(str);
+        return resp.Ip + " " + resp.Address;
     }
 
     public static bool IsUnix()
@@ -221,6 +220,13 @@ public static class ComputerUtil
     }
 }
 
+public class IpCnResp
+{
+    public string Ip { get; set; }
+
+    public string Address { get; set; }
+}
+
 /// <summary>
 /// 内存信息
 /// </summary>