更改 DefaultWebProxy 导致 WebRequest 超时

发布于 2024-08-13 09:43:02 字数 3265 浏览 2 评论 0原文

对于我正在从事的项目,我们有一个桌面程序,可以联系商店的在线服务器。因为它在学校中使用,所以正确设置代理是很棘手的。我们的目标是允许用户指定要使用的代理详细信息(如果他们愿意),否则将使用 IE 中的代理详细信息。我们还尝试绕过输入的错误详细信息,因此代码会尝试用户指定的代理,如果默认代理失败,则使用凭据,如果失败则为空。

我遇到的问题是,在需要连续更改代理设置的地方(例如,如果他们的注册由于代理错误而失败,他们会更改一件小事并重试,需要几秒钟。)我最终对 HttpRequests .GetResponse() 的调用超时,导致程序冻结很长一段时间。有时,如果我在更改之间留出一两分钟,它不会冻结,但不是每次都冻结(10 分钟后再次尝试,它再次超时)。

我在代码中找不到任何可能导致此问题的内容 - 尽管它看起来有点混乱。我认为服务器不会拒绝请求,除非它是通用服务器行为,因为我已经尝试过向我们的服务器和其他服务器(例如 google.co.uk)发出请求。

我发布代码是希望有人能够发现其中的问题,或者知道一种更简单的方法来完成我们正在尝试的事情。

我们运行的测试没有任何代理,因此通常会跳过第一部分。第一次运行 ApplyProxy 时,它工作正常并完成第一个 try 块中的所有内容,第二次,它可以在第一个 try 块中的 GetResponse 上超时,然后执行其余代码,或者它可以在那里工作并实际注册请求的超时。

代码:

void ApplyProxy() {

        Boolean ProxySuccess = true;
        String WebRequestURI = @"http://www.google.co.uk";

        if (UseProxy)
        {
            try
            {
                String ProxyUrl = (ProxyUri.ToLower().Contains("http://")) ?
                    ProxyUri :
                    "http://" + ProxyUri;

                WebRequest.DefaultWebProxy = new WebProxy(ProxyUrl);
                if (!string.IsNullOrEmpty(ProxyUsername) && !string.IsNullOrEmpty(ProxyPassword))
                    WebRequest.DefaultWebProxy.Credentials = new NetworkCredential(ProxyUsername, ProxyPassword);
                HttpWebRequest request = HttpWebRequest.Create(WebRequestURI) as HttpWebRequest;
                request.Method = "GET";
                HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            }
            catch
            {
                ProxySuccess = false;
            }
        }
        if(!ProxySuccess || !UseProxy)
        {
            try
            {
                WebRequest.DefaultWebProxy = WebRequest.GetSystemWebProxy();
                HttpWebRequest request = HttpWebRequest.Create(WebRequestURI) as HttpWebRequest;
                request.Method = "GET";
                HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            }
            catch (Exception e)
            { //try with credentials
                //make a new proxy from defaults
                WebRequest.DefaultWebProxy = WebRequest.GetSystemWebProxy();
                String newProxyURI = WebRequest.DefaultWebProxy.GetProxy(new Uri(WebRequestURI)).ToString();
                if (newProxyURI == String.Empty)
                { //check we actually get a result
                    WebRequest.DefaultWebProxy = null;
                    return;
                }
                //continue
                WebProxy NewProxy = new WebProxy(newProxyURI);
                NewProxy.UseDefaultCredentials = true;
                NewProxy.Credentials = CredentialCache.DefaultCredentials;
                WebRequest.DefaultWebProxy = NewProxy;

                try
                {
                    HttpWebRequest request = HttpWebRequest.Create(WebRequestURI) as HttpWebRequest;
                    request.Method = "GET";
                    HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                }
                catch
                {
                    WebRequest.DefaultWebProxy = null;
                }
            }

        }
    } 

For the project I'm working on, we have a desktop program that contacts an online server for a store. Because it's used in schools, getting the proxy setup right is tricky. What we've gone for is to allow users to specify proxy details to use if they want, otherwise it uses the ones from IE. We've also tried to bypass incorrect details being put in, so the code tries the user specified proxy, if that fails the default one, if that fails, then with credentials, if that fails then null.

The problem I'm having is that in places where the proxy settings need to be changed in succession (for example, if their registration fails because the proxy is wrong, they change one tiny thing and try again, takes seconds.) I end up with calls to a HttpRequests .GetResponse() timing out, causing the program to freeze for a good while. Sometimes if I leave a minute or two between the changes, it doesn't freeze, but not every time (Just tried again now after 10mins and it's timing out again).

I can't spot anything in the code that could cause this - though it looks a bit messy. I don't think it could be the server refusing the request unless it's generic server behaviour as I've tried this with requests to our server and others such as google.co.uk.

I'm posting the code in the hope that someone may be able to spot something that's wrong with it, or knows a much simpler way of doing what we're trying to.

The tests we run are without any proxy, so the first part is usually skipped. The first time ApplyProxy is run, it works fine and finishes everything in the first try block, the second, it can either timeout on the GetResponse in the first try block and then go through the rest of the code, or it can work there and timeout on the actual requests made for the registration.

Code:

void ApplyProxy()
{

        Boolean ProxySuccess = true;
        String WebRequestURI = @"http://www.google.co.uk";

        if (UseProxy)
        {
            try
            {
                String ProxyUrl = (ProxyUri.ToLower().Contains("http://")) ?
                    ProxyUri :
                    "http://" + ProxyUri;

                WebRequest.DefaultWebProxy = new WebProxy(ProxyUrl);
                if (!string.IsNullOrEmpty(ProxyUsername) && !string.IsNullOrEmpty(ProxyPassword))
                    WebRequest.DefaultWebProxy.Credentials = new NetworkCredential(ProxyUsername, ProxyPassword);
                HttpWebRequest request = HttpWebRequest.Create(WebRequestURI) as HttpWebRequest;
                request.Method = "GET";
                HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            }
            catch
            {
                ProxySuccess = false;
            }
        }
        if(!ProxySuccess || !UseProxy)
        {
            try
            {
                WebRequest.DefaultWebProxy = WebRequest.GetSystemWebProxy();
                HttpWebRequest request = HttpWebRequest.Create(WebRequestURI) as HttpWebRequest;
                request.Method = "GET";
                HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            }
            catch (Exception e)
            { //try with credentials
                //make a new proxy from defaults
                WebRequest.DefaultWebProxy = WebRequest.GetSystemWebProxy();
                String newProxyURI = WebRequest.DefaultWebProxy.GetProxy(new Uri(WebRequestURI)).ToString();
                if (newProxyURI == String.Empty)
                { //check we actually get a result
                    WebRequest.DefaultWebProxy = null;
                    return;
                }
                //continue
                WebProxy NewProxy = new WebProxy(newProxyURI);
                NewProxy.UseDefaultCredentials = true;
                NewProxy.Credentials = CredentialCache.DefaultCredentials;
                WebRequest.DefaultWebProxy = NewProxy;

                try
                {
                    HttpWebRequest request = HttpWebRequest.Create(WebRequestURI) as HttpWebRequest;
                    request.Method = "GET";
                    HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                }
                catch
                {
                    WebRequest.DefaultWebProxy = null;
                }
            }

        }
    } 

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

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

发布评论

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

评论(2

谎言月老 2024-08-20 09:43:02

这不只是需要设置 HttpWebRequest 的 Timeout 属性的情况吗?可能是正在建立连接,但未提供服务(例如,代理服务器类型错误或服务器停止运行),在这种情况下,请求可能在放弃之前等待超时时间 — 可能会出现更短的超时最好在这里。

Is it not just a case of needing to set the Timeout property of the HttpWebRequest? It could be that the connection is being made, but not serviced (wrong type of proxy server or stalled server, for example), in which case it may be that the request is waiting for the Timeout period before giving up — a shorter timeout may be preferrable here.

古镇旧梦 2024-08-20 09:43:02

对我来说似乎是一个编程错误。请求保持开放状态,显然程序或服务器不喜欢这样。完成后简单地关闭 HttpWebRequests 似乎可以消除这个问题。

Seems to be a programming error on my behalf. The requests were left open and obviously either the program or the server doesn't like this. Simply closing the HttpWebRequests once done seems to remove this issue.

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