To test if our computer is connected to a firewall we check if the loginwebpage for this firewall opens. If it times out, the error handling will say it's connected, if it opens (or throws a certificate error as it does in our case) we know we are not connected.
开发者_Go百科The big problem with this process is that whenever we are already connected, the application hangs until the webpage times out..
Any input is more than welcome, here's the code I use:
public static bool FirewallConnectivityStatus(string url)
{
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Proxy = null;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (HttpStatusCode.OK == response.StatusCode)
{
response.Close();
return false;
}
else
{
return true;
}
}
catch (WebException Ex)
{
if (Ex.Status == WebExceptionStatus.TrustFailure)
{
return false;
}
else
{
return true;
}
}
}
Update In the meantime I have found a socket connection that will allow me far better to create this connection via the IP in stead of the url: C# - Socket to log on to Firewall
Maybe using a BackgroundWorker could work:
http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx
They have quite a decent example on that page. I've never thought about trying to test this before. I cannot think of a simple way to test it. It looks like its been asked before as well:
C# API to test if a network Adapter is firewalled
At least the background worker could let your program continue while waiting for the timeout. It just depends if you can let the app continue at the point you are testing it without an answer.
How about reducing the timeout period? http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.timeout.aspx
精彩评论