C# - Testing Internet page - Waiting for timeout

孤者浪人 提交于 2019-12-13 05:16:40

问题


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.

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


回答1:


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



来源:https://stackoverflow.com/questions/6622847/c-sharp-testing-internet-page-waiting-for-timeout

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!