From b2f25bfca6471fe3bfa041da8e4a1d01fd00f6ed Mon Sep 17 00:00:00 2001 From: wxr <wxr@hdlchina.com.cn> Date: 星期一, 23 九月 2024 16:54:12 +0800 Subject: [PATCH] Update JLCountrycode.cs --- HDL-ON_Android/Other/JLCountrycode.cs | 40 ++++++++++++++++++++++++++++++++++++++++ 1 files changed, 40 insertions(+), 0 deletions(-) diff --git a/HDL-ON_Android/Other/JLCountrycode.cs b/HDL-ON_Android/Other/JLCountrycode.cs index 487892b..6590e00 100644 --- a/HDL-ON_Android/Other/JLCountrycode.cs +++ b/HDL-ON_Android/Other/JLCountrycode.cs @@ -4,6 +4,9 @@ using Com.Widget.Jlcountrycode.Sortselect; using System.Collections.Generic; using Java.Util; +using Newtonsoft.Json.Linq; +using System.Threading.Tasks; +using System.Net.Http; namespace JLCountrycode { @@ -27,6 +30,41 @@ return m_Current; } } + + + /// <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; + } + /// <summary> /// 鍥藉鍖哄彿閫夋嫨 @@ -125,4 +163,6 @@ } + + } \ No newline at end of file -- Gitblit v1.8.0