public IActionResult Index() { Program.RequestNumber++; string url = Environment.GetEnvironmentVariable("ApiUrl"); if (string.IsNullOrEmpty(url)) { url = _configuration.GetSection("ApiUrl").Value; } using (HttpClient client = new HttpClient()) { ResultIp result = new ResultIp(); try { client.BaseAddress = new Uri(url); MediaTypeWithQualityHeaderValue contentType = new MediaTypeWithQualityHeaderValue("application/json"); client.DefaultRequestHeaders.Accept.Add(contentType); HttpResponseMessage response = client.GetAsync("/api/LocalMachine").Result; string stringData = response.Content.ReadAsStringAsync().Result; WebApiResult data = JsonConvert.DeserializeObject <WebApiResult>(stringData); result.ApiIp = data.ApiIp; result.ApiMachineName = data.ApiMachineName; result.ApiOS = data.ApiOS; result.ApiRequestNumber = data.RequestNumber; } catch { result.ApiIp = "0.0.0.0"; result.ApiMachineName = "machine name"; result.ApiOS = "api os - " + url; result.ApiRequestNumber = 0; } IPHostEntry host; host = Dns.GetHostEntry(Dns.GetHostName()); foreach (IPAddress ip in host.AddressList) { if (ip.AddressFamily == AddressFamily.InterNetwork) { result.AppIp = ip.ToString(); result.AppMachineName = Environment.MachineName; result.AppOS = Environment.OSVersion.VersionString; result.AppRequestNumber = Program.RequestNumber; } } return(View(result)); } }
private ResultIpData GetIpAddress(string Ips) { ResultIpData resdata = new ResultIpData(); String querys = "ip=" + Ips + ""; String bodys = ""; String url = host + path; HttpWebRequest httpRequest = null; HttpWebResponse httpResponse = null; if (0 < querys.Length) { url = url + "?" + querys; } if (host.Contains("https://")) { ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); httpRequest = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url)); } else { httpRequest = (HttpWebRequest)WebRequest.Create(url); } httpRequest.Method = method; httpRequest.Headers.Add("Authorization", "APPCODE " + appcode); if (0 < bodys.Length) { byte[] data = Encoding.UTF8.GetBytes(bodys); using (Stream stream = httpRequest.GetRequestStream()) { stream.Write(data, 0, data.Length); } } try { httpResponse = (HttpWebResponse)httpRequest.GetResponse(); } catch (WebException ex) { httpResponse = (HttpWebResponse)ex.Response; } if (httpResponse.StatusCode == HttpStatusCode.OK) { Stream st = httpResponse.GetResponseStream(); StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8")); string strs = reader.ReadToEnd(); ResultIp resIp = JsonConvert.DeserializeObject <ResultIp>(strs); if (resIp.code == 0 && resIp.data != null) { resdata = JsonConvert.DeserializeObject <ResultIpData>(resIp.data.ToString()); } else { iLog.WriteLog("GetIpAddress:处理异常:" + resIp.data.ToString(), 1); } } else if (httpResponse.StatusCode == HttpStatusCode.Forbidden) { System.Threading.Thread.Sleep(5000); } return(resdata); }