| | |
| | | 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 |
| | | { |
| | |
| | | return m_Current; |
| | | } |
| | | } |
| | | |
| | | |
| | | /// <summary> |
| | | /// 根据手机当前的IP获取国家信息 |
| | | /// </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> |
| | | /// 国家区号选择 |
| | |
| | | |
| | | } |
| | | |
| | | |
| | | |
| | | } |