Created
December 22, 2012 02:32
-
-
Save alexsandro-xpt/4357148 to your computer and use it in GitHub Desktop.
Internet check experiment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace ConsoleApplication1 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
if (!InternetConnection.HasProxy() && InternetConnection.HasInternet()) | |
{ | |
Console.WriteLine("Interent liberada"); | |
} | |
else if (InternetConnection.HasProxy() && InternetConnection.HasInternet()) | |
{ | |
Console.WriteLine("Interent com proxy"); | |
} | |
else | |
{ | |
Console.WriteLine("Não tem internet " + InternetConnection.LastConnectionErro); | |
} | |
Console.ReadKey(); | |
} | |
} | |
} | |
///////---------------------------------- | |
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Net; | |
using System.Security.Policy; | |
using System.Text; | |
namespace ConsoleApplication1 | |
{ | |
public class InternetConnection | |
{ | |
static readonly IWebProxy _proxy = WebProxy.GetDefaultProxy(); | |
public static ProxyProperts Proxy { get; private set; } | |
public static string LastConnectionErro { get; private set; } | |
public class ProxyProperts | |
{ | |
public string Host { get; set; } | |
public int Port { get; set; } | |
} | |
static InternetConnection() | |
{ | |
var pr = ((WebProxy)_proxy); | |
if (pr.Address != null && pr.Address.AbsoluteUri != string.Empty) | |
{ | |
Proxy = new ProxyProperts { Host = pr.Address.Host, Port = pr.Address.Port }; | |
} | |
if (string.IsNullOrEmpty(CheckIp())) | |
{ | |
if (HasProxy()) | |
{ | |
try | |
{ | |
CheckInternet(); | |
} | |
catch (WebException e) | |
{ | |
LastConnectionErro = e.Message; | |
} | |
} | |
} | |
} | |
public static bool HasProxy() | |
{ | |
return Proxy != null; | |
} | |
public static IWebProxy GetProxy() | |
{ | |
return _proxy; | |
} | |
private static string CheckInternet() | |
{ | |
using (var wc = new WebClient()) | |
{ | |
if (HasProxy()) wc.Proxy = GetProxy(); | |
string result = wc.DownloadString("http://checkip.dyndns.com"); | |
return ParseCheckResult(result); | |
} | |
} | |
public static bool HasInternet() | |
{ | |
return !string.IsNullOrEmpty(CheckIp()); | |
} | |
public static string CheckIp() | |
{ | |
string ipString; | |
using (var wc = new WebClient()) | |
{ | |
if (HasProxy()) wc.Proxy = GetProxy(); | |
try | |
{ | |
string result = wc.DownloadString("http://checkip.dyndns.com"); | |
return ParseCheckResult(result); | |
} | |
catch (Exception) | |
{ | |
ipString = string.Empty; | |
} | |
} | |
return ipString; | |
} | |
private static string ParseCheckResult(string result) | |
{ | |
try | |
{ | |
string temp = result.Substring(result.IndexOf(":", System.StringComparison.Ordinal) + 2); | |
temp = temp.Remove(temp.IndexOf("<", System.StringComparison.Ordinal)); | |
IPAddress address; | |
if (!IPAddress.TryParse(temp, out address)) temp = string.Empty; | |
return temp; | |
} | |
catch (Exception) | |
{ | |
return string.Empty; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment