private async void GetInfoFromURL(string ipAddr, int timeout = 10000) { ipinfo = null; bool cancelled = false; string jsonStr = null; using (WebClient wc = new WebClient()) { wc.DownloadStringCompleted += (s, e) => { if (e.Error != null || e.Cancelled) { cancelled = true; jsonStr = null; ipinfo = null; textIP.Text = "ERROR"; } else { jsonStr = e.Result; ipinfo = JsonSerializer.Deserialize <IPInfo.IPInfo>(jsonStr); SaveInfoToFile(ipinfo); } ShowResult(ipAddr); }; Uri uri = new Uri($"http://free.ipwhois.io/json/{ipAddr}"); wc.DownloadStringAsync(uri); // // ожидаем ответа var currentTime = DateTime.Now; while (jsonStr == null && !cancelled && DateTime.Now.Subtract(currentTime).TotalMilliseconds < timeout) { await Task.Delay(100); } } }
private void SaveInfoToFile(IPInfo.IPInfo info = null) { if (info != null) { infoList.Add(info); } string serializedText = JsonSerializer.Serialize(infoList); File.WriteAllText(FILENAME, serializedText); }
private void GetInfoFromFile(string ipAddr) { ipinfo = null; foreach (IPInfo.IPInfo i in infoList) { if (i.ip == ipAddr) { i.success = true; ipinfo = i; } } }