From 4dc8adc2bf82f1827324c4dbefbc4477202bc433 Mon Sep 17 00:00:00 2001 From: wxr <wxr@hdlchina.com.cn> Date: 星期一, 23 九月 2024 16:53:33 +0800 Subject: [PATCH] 服务器自动选择;i --- HDL-ON_iOS/Other/JLCountrycode.cs | 36 ++++++++++++++++++++++++++++++++++++ 1 files changed, 36 insertions(+), 0 deletions(-) diff --git a/HDL-ON_iOS/Other/JLCountrycode.cs b/HDL-ON_iOS/Other/JLCountrycode.cs index 176a7df..0346b43 100644 --- a/HDL-ON_iOS/Other/JLCountrycode.cs +++ b/HDL-ON_iOS/Other/JLCountrycode.cs @@ -2,6 +2,9 @@ using Shared.IOS.JLCountryCode; using Foundation; using System.Collections.Generic; +using Newtonsoft.Json.Linq; +using System.Net.Http; +using System.Threading.Tasks; namespace JLCountrycode { @@ -95,6 +98,39 @@ return newDictionary; } + /// <summary> + /// 鏍规嵁鎵嬫満褰撳墠鐨処P鑾峰彇鍥藉淇℃伅 + /// </summary> + /// <returns></returns> + public async Task<string> GetCountryByIP() + { + string country = "Unknown"; + + try + { + using (HttpClient client = new HttpClient()) + { + // 浣跨敤 ipinfo.io 鑾峰彇浣嶇疆淇℃伅 + HttpResponseMessage response = await client.GetAsync("https://ipinfo.io/json"); + + if (response.IsSuccessStatusCode) + { + string json = await response.Content.ReadAsStringAsync(); + JObject jsonObject = JObject.Parse(json); + + // 浠庤繑鍥炵殑 JSON 涓彁鍙栧浗瀹朵俊鎭� + country = jsonObject["country"].ToString(); + } + } + } + catch (Exception ex) + { + Console.WriteLine($"Error fetching IP info: {ex.Message}"); + } + + return country; + } + } -- Gitblit v1.8.0