Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 更改DefaultWebProxy导致WebRequests超时_C#_Proxy_Timeout - Fatal编程技术网

C# 更改DefaultWebProxy导致WebRequests超时

C# 更改DefaultWebProxy导致WebRequests超时,c#,proxy,timeout,C#,Proxy,Timeout,对于我正在进行的项目,我们有一个桌面程序,可以联系商店的在线服务器。因为它在学校中使用,所以要正确设置代理是很困难的。我们所做的是允许用户指定要使用的代理详细信息,否则它将使用IE中的代理详细信息。我们还尝试绕过输入的错误详细信息,因此代码将尝试用户指定的代理,如果默认代理失败,如果失败,则使用凭据,如果失败,则为null 我遇到的问题是,在需要连续更改代理设置的地方(例如,如果代理错误导致注册失败,他们会更改一件小事,然后重试,需要几秒钟)。我最终会调用HttpRequests.GetResp

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

我遇到的问题是,在需要连续更改代理设置的地方(例如,如果代理错误导致注册失败,他们会更改一件小事,然后重试,需要几秒钟)。我最终会调用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;
                }
            }

        }
    } 

这不仅仅是需要设置HttpWebRequest的Timeout属性的情况吗?可能是正在建立连接,但未提供服务(例如,错误类型的代理服务器或已停止的服务器),在这种情况下,可能是请求正在等待超时时间,然后才放弃-这里最好缩短超时时间。

这似乎是我的编程错误。请求被保留为打开状态,显然程序或服务器不喜欢这样。一旦完成,只需关闭HttpWebRequests就可以删除请求他的问题。

不幸的是,请求没有得到服务,以注册为例,当出现成功或失败消息时,什么都不会返回。