什么是测试代理设置的首选或接受的方法? [英] What is the prefered or accepted method for testing proxy settings?

查看:88
本文介绍了什么是测试代理设置的首选或接受的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有在程序中的互联网连接了不少麻烦我的工作,这一切似乎从某些问题的代理服务器设置产卵。大多数在这一点上的问题是固定的,但我现在遇到的问题是,我测试的代理服务器设置的方法使得一些用户等待很长一段时间。

I have a lot of trouble with the internet connectivity in the program I am working on and it all seems to spawn from some issue with the proxy settings. Most of the issues at this point are fixed, but the issue I am having now is that my method of testing the proxy settings makes some users wait for long periods of time.

这是我做的:

System.Net.WebClient webClnt = new System.Net.WebClient();

webClnt.Proxy = proxy;
webClnt.Credentials = proxy.Credentials;

byte[] tempBytes;
try
{
    tempBytes = webClnt.DownloadData(url.Address);
}
catch
{
    //Invalid proxy settings

    //Code to handle the exception goes here
}

这是我发现来测试代理设置是否正确的唯一途径。我试图使Web服务调用我们的Web服务,但在致电时不需要代理设置。如果我有虚假代理服务器设置,它仍然工作。以上方法,不过,有我可以设置我能找到没有超时成员,我使用DownloadData相对于DownloadDataAsync因为我需要等待直到该方法是这样做的,我可以知道设置是否继续之前正确在该程序。

This is the only way that I've found to test if the proxy settings are correct. I tried making a web service call to our web service, but no proxy settings are needed when making the call. It will work even if I have bogus proxy settings. The above method, though, has no timeout member that I can set that I can find and I use the DownloadData as opposed to the DownloadDataAsync because I need to wait til the method is done so that I can know if the settings are correct before continuing on in the program.

这是一个更好的方法或该方法的变通任何的建议表示赞赏。

Any suggestions on a better method or a work around for this method is appreciated.

迈克

编辑:我试过别的东西,但没有运气。我用DownloadDataAsync方法来下载一个单独的线程结束后,即提高了Web客户端的DownloadDataCompleted事件的数据。虽然我等待被调用的事件我有一个循环:while(!DateTime.Now< downloadStart.AddMinutes(超时)及和放大器; TestIsDone){}该事件DownloadDataCompleted设置TestIsDone成员为true时,称为事件。这里的问题是,如果代理服务器设置是坏的事件不会被调用,不会抛出异常,程序等待继续之前整个超时时间。以下是此方法的代码:

I tried something else, but no luck. I used the DownloadDataAsync method to download the data in a separate thread which raises the DownloadDataCompleted event of the WebClient when finished. While I wait for the event to get called I have a loop: while(DateTime.Now < downloadStart.AddMinutes(timeout) && !TestIsDone) {} The DownloadDataCompleted event sets the TestIsDone member to true when the event is called. The problem here is if the proxy settings are bad the Event never gets called, no exception is thrown, and the program waits for the entire timeout period before continuing. Here is the code for this approach:

public static bool TestProxy(System.Net.WebProxy proxy)
{
    ProxySettingsTestDone = false; //public static var
    string address = //url to some arbitrary data on our server

    System.Net.WebClient webClnt = new System.Net.WebClient();

    webClnt.Proxy = proxy;
    webClnt.Credentials = proxy.Credentials;

    try
    {
        webClnt.DownloadDataCompleted += new System.Net.DownloadDataCompletedEventHandler(DownloadDataCallback);
        webClnt.DownloadDataAsync(new Uri(address));

        //Timeout period
        DateTime dnldStartTime = DateTime.Now;
        while (DateTime.Now < dnldStartTime.AddMinutes(1.0) && !ProxySettingsTestDone)
        { }

        if (!ProxySettingsTestDone) //Exceded timeout
        {
            throw new System.Net.WebException("Invalid Proxy Settings");
        }
    }
    catch (System.Net.WebException e)
    {
        if (e.Status == System.Net.WebExceptionStatus.ProxyNameResolutionFailure)
        {
            //Proxy failed, server may or may not be there
            Util.ConnectivityErrorMsg = e.Message;
            return false;
        }
        else if (e.Status == System.Net.WebExceptionStatus.ProtocolError)
        {
            //File not found, server is down, but proxy settings succeded
            ServerUp = false;
            Util.ConnectivityErrorMsg = e.Message;
            return true;
        }

        return false;
    }

    Util.ConnectivityErrorMsg = "";
    return true;
}

private static void DownloadDataCallback(object sender, System.Net.DownloadDataCompletedEventArgs e)
{
    if (!e.Cancelled && e.Error == null)
        ProxySettingsTestDone = true;
    else
        throw new System.Net.WebException("Invalid Proxy Settings");
}



很抱歉的长期职位。我想更新与我测试这种新方法后,找到的信息这个问题。

Sorry about the long post. I wanted to update this question with the information that I found after testing this new approach.

谢谢,
迈克

推荐答案

您可以运行在一个单独的线程proxycheck。 。并考虑如果线程的时间太长进行故障检查

You can run the proxycheck in a seperate thread. And consider the check to be failed if the thread takes too long.

或者你可以使用WebRequest的,它可以让你设置超时:

Or you could use WebRequest, it allows you set a timeout:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://url");
request.Proxy = proxy;
request.Timeout = 2000;

如果该请求没有指定超时时间内完成了引发WebException 状态属性设置为 WebExceptionStatus.Timeout 将被抛出。

If the request has not finished within the given timeout a WebException with the Status property set to WebExceptionStatus.Timeout will be thrown.

这篇关于什么是测试代理设置的首选或接受的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆