private async void GeoIPTest_Click(object sender, EventArgs e) { StartBtn.Enabled = false; //GeoIPBtn.Enabled = false; foreach (ListViewItem item in NodeList.Items) { NodeConfig node = (NodeConfig)item.Tag; var geoipItem = item.SubItems["GeoIPItem"]; // 跳过已ping if ((bool)geoipItem.Tag == true) { continue; } geoipItem.Text = "测试中"; IPSBObject GeoIPResult = await GetGeoIPAsync(node.server); // Request country if (GeoIPResult == null || GeoIPResult.country == string.Empty) { geoipItem.Text = "失败"; } else { geoipItem.Text = GeoIPResult.country; // update node } geoipItem.Tag = true; } StartBtn.Enabled = true; //GeoIPBtn.Enabled = true; }
private async Task <IPSBObject> RequestIPSBAPI(string ip) { bool flag = false; // dns resolve ok flag try { IPAddress ipTry = IPAddress.Parse(ip); // detect if ip address } catch { try { IPHostEntry host = Dns.GetHostEntry(ip); // try to resolve domain } catch { flag = true; } if (!flag) { IPAddress ResultIPList = Dns.GetHostEntry(ip).AddressList[0]; // save resolve result ip = ResultIPList.ToString(); } } HttpClient client = new HttpClient(); try { HttpResponseMessage response = await client.GetAsync("https://api.ip.sb/geoip/" + ip); // call IP.SB API if (response.IsSuccessStatusCode) { var resultJSONText = response.Content.ReadAsStringAsync().Result; IPSBObject resultJSONObj = JsonConvert.DeserializeObject <IPSBObject>(resultJSONText); return(resultJSONObj); // return ok result } } catch (Exception ex) { MessageBox.Show(ex.Message); } return(null); // return error result }