Search code examples
c#.netexceptionproxyskip

C# skip all proxy exceptions - time-out


Is there any way i can skip/catch all proxy exceptions? And also maybe put a time-out so the program wont get stuck in-between

webProxy = new WebProxy("" + prox + "");
webProxy.Credentials = CredentialCache.DefaultCredentials;
wr.Proxy = webProxy;

I've added

             catch (Exception ex)
            {
                // Do nothing or log
                var exceptio = ex.ToString();
                richTextBox1.Text = exceptio;
            }

how can i put a time-out on it?


Solution

  • As suggested you need to enclose the executing code in a try/catch block.
    You can fiddle around with debugger Exception handling under Debug/Exceptions... (in Visual Studio) but regardless, any non-handled exception will always trigger the debugger to break.

    try
    {
        // Do work that might fail
    }
    catch (Exception ex)
    {
        // Do nothing or log
        Trace.WriteLine(ex);
    }
    

    More on debugging and exceptions can be found here

    Regarding time-out, you put it on the WebRequest object, not on the proxy, like so:

    WebProxy webProxy = new WebProxy("http://myproxyserver:80/");
    WebRequest webRequest = WebRequest.Create("http://www.stackoverflow.com");
    webRequest.Proxy = webProxy;
    webRequest.Timeout = 5000;  // <-- Set time out here, in milliseconds
    ...
    

    Read more on timeout here.