/// <summary> /// 获取IP信息 /// </summary> /// <param name="ip"></param> /// <returns></returns> public static IpResult GetIpResult(string ip) { IpResult info = null; try { info = GetIpResultByCache(ip, info); if (info == null) { //替换原有的淘宝服务 info = GeIpResultByApiTB(ip); if (info == null) { string url = string.Format("http://ip.taobao.com/service/getIpInfo.php?ip={0}", ip); string json = DN.Framework.Utility.WebClientHelper.GetSend(url); info = DN.Framework.Utility.Serializer.DeserializeObject <IpResult>(json); if (info.code == 0) { GetIpResultByCache(ip, info); } } else { GetIpResultByCache(ip, info); } } } catch (Exception ex) { DN.Framework.Utility.LogHelper.Write(ex.Message, "error"); } return(info); }
public static IpResult GeIpResultByApiTB(string ip) { IpResult result = null; try { String querys = "ip=" + ip; 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; } Stream st = httpResponse.GetResponseStream(); StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8")); string json = reader.ReadToEnd(); if (!string.IsNullOrEmpty(json)) { result = DN.Framework.Utility.Serializer.DeserializeObject <IpResult>(json); } } catch (Exception ex) { DN.Framework.Utility.LogHelper.Write(string.Format("{0},转换错误,:{1}", ip, ex.Message), "iperror"); } return(result); }
public async Task <IpResult> GetIpInfo(string ipAddress) { var url = $"{__BASE_URL}{ipAddress}?api_key={__API_KEY}"; var response = await _httpClient.GetAsync(url); var json = await response.Content.ReadAsStringAsync(); IpResult result = json.ToIpResult(); result.JsonContent = json; return(result); }
public static void ToFile(this IpResult model, string path) { if (model == null) { throw new ArgumentNullException("The model is null."); } using (var writer = new StreamWriter(path, append: true, encoding: Encoding.UTF8)) { writer.Write(model.ToDisplay()); writer.Flush(); writer.Close(); } }
//Function to call when the library will automatically fetch the next IP Address and pass the value of the hostname public async Task <HostRecord> CreateHostRecordAsync(string HostName, string HostMac = null) { //Validations(2) // Check#1 - Validate input to ensure that a hostname was supplied if (String.IsNullOrEmpty(HostName)) { return(default(HostRecord)); } IpResult nextIP = GetIPAsync(1).Result; return(await CreateHostRecordAsync(HostName, nextIP.IPAddresses[0], null)); }
static IpResult GetIpResultByCache(string ip, IpResult ipinfo) { if (m_list.ContainsKey(ip)) { ipinfo = m_list[ip]; } else { if (ipinfo != null) { m_list.Add(ip, ipinfo); } } return(ipinfo); }
public async Task <IpResult> GetIPAsync(int totalIPRequested = 1, string subnetIp = "") { // https://10.10.10.10/wapi/v2.9/network/ZG5zLm5ldHdvcmskMTAuMTI4LjAuMC8yNC8w?_function=next_available_ip if (totalIPRequested <= 0) { return(default(IpResult)); } string apifunction = "network"; string refnetwork = FindSubnetBaseRef(subnetIp); string apicommand = "_function=next_available_ip"; string apipath = $"{helperConfig.ApiRoute}/{helperConfig.ApiVersion}/{apifunction}/{refnetwork}"; string iprequested = new IpRequest(totalIPRequested).ToJson(); string content = await IBXCallApi(HttpMethod : HttpMethod.Post, ApiFunction : apifunction, ApiPath : apipath, ApiCommand : apicommand, RequestContent : iprequested); return(IpResult.FromJson(content)); }
public static string ToDisplay(this IpResult model, string header = "") { var sb = new StringBuilder(); sb.AppendLine(header); try { sb.AppendLine($"Get info for IP : {model.Ip}"); sb.AppendLine("--------------------------------------------------------"); sb.AppendLine($"host : {model.Hostname}"); sb.AppendLine($"ip : {model.Ip}"); sb.AppendLine($"ip-type : {model.IpType}"); sb.AppendLine($"requester-ip : {model.RequesterIp}"); sb.AppendLine($"Status code : {model.StatusCode}"); if (model.Asn != null) { sb.AppendLine("********** ISP info :"); sb.AppendLine($"name : {model.Asn.Name}"); sb.AppendLine($"type : {model.Asn.Type}"); sb.AppendLine($"domain : {model.Asn.Domain}"); sb.AppendLine($"organization : {model.Asn.Organization}"); } if (model.Geo != null) { sb.AppendLine("********** GEO info :"); sb.AppendLine($"continent-code : {model.Geo.ContinentCode}"); sb.AppendLine($"continent-name : {model.Geo.ContinentName}"); sb.AppendLine($"country : {model.Country.ContinentName}"); sb.AppendLine($"country iso : {model.Country.CountryIsoCode}"); sb.AppendLine($"zip-code : {model.Geo.ZipCode}"); sb.AppendLine($"capital : {model.Country.Capital}"); sb.AppendLine($"is-metric : {model.Geo.IsMetric}"); sb.AppendLine($"longitude : {model.Geo.Longitude}"); sb.AppendLine($"latitude : {model.Geo.Latitude}"); sb.AppendLine($"country-geo-id : {model.Geo.CountryGeoId}"); } if (model.Timezone != null) { sb.AppendLine("********** Timezone :"); sb.AppendLine($"{model.Timezone.IanaName}"); sb.AppendLine($"name : {model.Timezone.MicrosoftName}"); sb.AppendLine($"day-light-saving : {model.Timezone.IsDaylightSaving}"); } if (model.Security != null) { sb.AppendLine("********** Security :"); sb.AppendLine($"is-crawler : {model.Security.IsCrawler}"); sb.AppendLine($"is-tor : {model.Security.IsTor}"); sb.AppendLine($"is-proxy : {model.Security.IsProxy}"); } sb.AppendLine(Environment.NewLine); } catch { sb.AppendLine($"_____________Error : cannot parse info."); } return(sb.ToString()); }