C# 跳过所有代理异常 - 超时

发布于 2024-11-10 05:37:16 字数 450 浏览 1 评论 0原文

有什么方法可以跳过/捕获所有代理异常吗? 也许还可以设置一个超时,这样程序就不会卡在中间

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

我已经添加了

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

如何设置一个超时?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凯凯我们等你回来 2024-11-17 05:37:16

try/catch 块对此有帮助吗?

Would a try/catch block help with this?

往事随风而去 2024-11-17 05:37:16

正如建议的,您需要将执行代码包含在 try/catch 块中。
您可以在调试/异常...(在 Visual Studio 中)下摆弄调试器异常处理,但无论如何,任何未处理的异常将始终触发调试器中断。

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

有关调试和异常的更多信息,请参见此处

关于超时,您将其放在 WebRequest 对象上,而不是放在代理上,如下所示:

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
...

阅读有关超时的更多信息 此处

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文