I'm creating a internet connection utility and below is the code I have used.
Is there any better way of check for proxy and internet connection etc?
/// <summary>
/// Check the Internet connection using the url passed into it
/// Network Adapter Check - TRUE
/// Proxy Settings Check - TRUE
/// Speed Check - Depends on Parameter speedCheck
/// Internet Connection Check - TRUE
/// </summary>
/// <param name="url">Url to validate connection</param>
/// <param name="speedCheck">Validate Speed of Interenet Connection </param>
/// <returns>Connection/No Connection</returns>
public static bool CheckInternet(string url, bool speedCheck)
{
speedCheckRequired = speedCheck;
setProgressBar("Internet Connection", "Validation", 4);
bool present = validateConnection(url);
closeProgressBar();
return present;
}
/// <summary>
/// Check for proxy setting and if pressent gather proxy details
/// </summary>
/// <param name="web">WebClient Obeject</param>
/// <returns>Setting Found/Not Found</returns>
public static bool IsProxyRequired(string testUrl)
{
try
{
progressBar.SetStatus("Quering for proxy settings");
HttpWebRequest myWebRequest = (HttpWebRequest)WebRequest.Create(testUrl);
IWebProxy proxy = myWebRequest.Proxy;
var settings = proxy.GetProxy(myWebRequest.RequestUri);
if (!settings.OriginalString.Equals(testUrl))
{
Utilities.DebugSolution.SetMessage("Utilities.NetworkCommunication.IsProxyRequired","Proxy setting found");
Utilities.DebugSolution.SetMessage("Utilities.NetworkCommunication.IsProxyRequired", String.Format("Url: {0} Port: {1}",settings.AbsoluteUri,settings.Port));
proxySettings = proxy;
return true;
}
}
catch (Exception e)
{
Console.WriteLine("Utilities.NetworkCommunication.IsProxyRequired Exception: " + e.Message.ToString());
Utilities.DebugSolution.SetException("Utilies.NetworkCommunication.IsProxyRequired", e);
}
return false;
}
/// <summary>
/// Make sure there is a network connection on the device
/// </summary>
/// <param name="status"></param>
/// <returns>Connected/Not Connected</returns>
public static bool IsNetorkAdapterConnected(ref string status)
{
bool result = false;
status = "Network adapter not connected";
try
{
progressBar.SetStatus("Quering network connections");
if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())
{
result = true;
status = String.Empty;
}
}catch (Exception e)
{
Console.WriteLine("Utilities.NetworkCommunication.IsNetorkAdapterConnected Exception: " + e.Message.ToString());
Utilities.DebugSolution.SetException("Utilies.NetworkCommunication.IsNetorkAdapterConnected", e);
}
return result;
}
private static bool validateConnection(string url)
{
string status = String.Empty;
if (IsNetorkAdapterConnected(ref status))
{
try
{
using (var client = new WebClient())
{
if (IsProxyRequired(url))
client.Proxy = proxySettings;
try
{
progressBar.SetStatus("Checking internet connection");
using (var stream = client.OpenRead(url))
return CheckSpeed(String.Empty);
}catch
{
///This Catch says there is no internet connection available.
///So no login\debug set here!
}
}
}
catch (Exception e)
{
Console.WriteLine("Utilities.NetworkCommunication.validateConnection Exception: " + e.Message.ToString());
Utilities.DebugSolution.SetException("Utilies.NetworkCommunication.validateConnection", e);
}
}//END if (IsNetorkAdapterConnected(ref status))
return false;
}