/// <summary> /// Gets your external router IP address from a public web IP API. /// <para>See System.Net.WebClient.DownloadString(string) on MSDN for exception info.</para> /// </summary> /// <param name="api">The API to get your IP Address from.</param> /// <param name="timeOut">The timeout in milliseconds before the function fails.</param> /// <returns>Returns a new IPAddress with the resuling IP address from the IP API, else null on fail/timeout.</returns> /// <exception cref="System.ArgumentNullException"/> /// <exception cref="System.Net.WebException"/> /// <exception cref="System.NotSupportedException"/> public virtual IPAddress IPFromAPI(IPAPI api, int timeOut) { try { using (WebTimeoutClient client = new WebTimeoutClient(timeOut)) { string webAddress = null, response = string.Empty; IPAddress address = null; switch (api) { case IPAPI.IPIfy: webAddress = webIPify; break; case IPAPI.IPInfo: webAddress = webIPInfo; break; case IPAPI.IPAPI: response = client.DownloadString(webIPAPI); int index = response.IndexOf("query") + 8; int length = response.IndexOf("region") - 3 - index; if (IPAddress.TryParse(response, out address)) { return(address); } return(null); case IPAPI.CheckIPDyDNS: response = client.DownloadString(webCheckIPDynDNS); string IP = new Regex(@"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}").Match(response).Value; if (IPAddress.TryParse(response, out address)) { return(address); } return(null); } response = client.DownloadString(webAddress); if (IPAddress.TryParse(response, out address)) { return(address); } return(null); } } catch (WebException ex) { if (ex.Status == WebExceptionStatus.Timeout) { return(null); } throw ex; } }
/// <summary> /// Gets your external router IP address from a public web IP API. /// <para>See System.Net.WebClient.DownloadString(string) on MSDN for exception info.</para> /// </summary> /// <param name="api">The API to get your IP Address from.</param> /// <param name="timeOut">The timeout in milliseconds before the function fails.</param> /// <returns>Returns a new IPAddress with the resuling IP address from the IP API, else null on fail/timeout.</returns> /// <exception cref="System.ArgumentNullException"/> /// <exception cref="System.Net.WebException"/> /// <exception cref="System.NotSupportedException"/> public virtual IPAddress IPFromAPI( IPAPI api, int timeOut ) { try { using( WebTimeoutClient client = new WebTimeoutClient( timeOut ) ) { string webAddress = null, response = string.Empty; IPAddress address = null; switch( api ) { case IPAPI.IPIfy: webAddress = webIPify; break; case IPAPI.IPInfo: webAddress = webIPInfo; break; case IPAPI.IPAPI: response = client.DownloadString( webIPAPI ); int index = response.IndexOf( "query" ) + 8; int length = response.IndexOf( "region" ) - 3 - index; if ( IPAddress.TryParse( response, out address ) ) return address; return null; case IPAPI.CheckIPDyDNS: response = client.DownloadString( webCheckIPDynDNS ); string IP = new Regex( @"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}" ).Match( response ).Value; if ( IPAddress.TryParse( response, out address ) ) return address; return null; } response = client.DownloadString( webAddress ); if ( IPAddress.TryParse( response, out address ) ) return address; return null; } } catch( WebException ex ) { if ( ex.Status == WebExceptionStatus.Timeout ) return null; throw ex; } }