当前位置:编程学习 > C#/ASP.NET >>

WebRequest.GetRequestStream 超时不起作用

本人有段WebRequest使用HTTP/HTTPS代理请求页面的代码,代码已经做了超时设置,对使用大部分带来请求数据的时候超时设置都起作用,但是在使用很少部分HTTPS代理的时候,超时设置根本不请任何作用,WebRequest.GetRequestStream 无限等待,不知道为什么会出现这种情况。请大家帮下忙


string htmlResult = "";
            try
            {
                ServicePointManager.Expect100Continue = false;
                HttpWebRequest request = null;
                if (strURL.StartsWith("https", StringComparison.OrdinalIgnoreCase))
                {
                    ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                    request = WebRequest.Create(strURL) as HttpWebRequest;
                }
                else
                {
                    request = WebRequest.Create(strURL) as HttpWebRequest;
                }
                request.MaximumAutomaticRedirections = 5;
                request.Timeout = maxReadMs;
                request.ReadWriteTimeout = maxReadMs;
                request.AllowAutoRedirect = redirect302;
                if (proxy != null && !string.IsNullOrEmpty(proxy.Host))
                {
                    request.Proxy = new WebProxy(proxy.Host, proxy.Port);
                    request.Timeout = 0x1770;
                }
                request.Method = getOrPost;
                request.Referer = referer;
                request.ContentType = "application/x-www-form-urlencoded";
                request.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)";
                request.Accept = "text/html, application/xhtml+xml, */*";
                if (usegzip)
                    request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
                if (!string.IsNullOrEmpty(appendHttpHeads))
                {
                    appendHttpHeads += "\r\n";
                    MatchCollection heads = new Regex(".*?\r\n").Matches(appendHttpHeads);
                    foreach (Match head in heads)
                    {
                        if (!string.IsNullOrEmpty(head.Value.Trim()))
                            request.Headers.Add(head.Value.Trim());
                    }
                }
                if (cookie != null)
                {
                    request.CookieContainer = new CookieContainer();
                    request.CookieContainer.Add(cookie.ToCookieCollection());
                }
                try
                {
                    if (!string.IsNullOrEmpty(postData))
                    {
                        byte[] data = Encoding.UTF8.GetBytes(postData);
                        request.ContentLength = data.Length;
                        using (Stream stream = request.GetRequestStream())
                        {
                            stream.Write(data, 0, data.Length);
                        }
                    }

                    using (WebResponse response = (HttpWebResponse)request.GetResponse())
                    {
                        Stream stream = response.GetResponseStream();
                        if (response.Headers[HttpResponseHeader.ContentEncoding] != null && response.Headers[HttpResponseHeader.ContentEncoding].ToLower().Contains("gzip"))
                            stream = new GZipStream(stream, CompressionMode.Decompress);
                        else if (response.Headers[HttpResponseHeader.ContentEncoding] != null && response.Headers[HttpResponseHeader.ContentEncoding].ToLower().Contains("deflate"))
                            stream = new DeflateStream(stream, CompressionMode.Decompress);
                        string charset = "";
                        if (!string.IsNullOrEmpty(response.ContentType))
                        {
                            if (new Regex("charset=", RegexOptions.IgnoreCase).IsMatch(response.ContentType))
                                charset = new Regex("charset=.*?[;| ]", RegexOptions.IgnoreCase).Match(response.ContentType + " ").Value.Split('=')[1].Trim().Trim(';');
                        }
                        if (string.IsNullOrEmpty(charset))
                            charset = "utf-8";
                        StreamReader reader = new StreamReader(stream, Encoding.GetEncoding(charset));
                        htmlResult = reader.ReadToEnd();
                        strURL = response.ResponseUri.ToString();
                        referer = strURL;
                        string cookiesHeaders = response.Headers[HttpResponseHeader.SetCookie];
                        if (!string.IsNullOrEmpty(cookiesHeaders))
                            cookie.Add(cookiesHeaders, response.ResponseUri.Host);
                        reader.Close();
                        reader.Dispose();
                        stream.Dispose();
                    }
                }
                catch (WebException e)
                {
                    using (WebResponse response = e.Response)
                    {
                        if (response != null)
                        {
                            HttpWebResponse httpResponse = (HttpWebResponse)response;
                            switch (httpResponse.StatusCode)
                            {
                                case HttpStatusCode.RequestTimeout: break;
                                default: break;
                            }
                        }
                    }
                }
            }
            catch { }
            return htmlResult;
--------------------编程问答-------------------- 请求的数据编码是什么?
要和发送的   request.ContentType = "application/x-www-form-urlencoded";编码一致 --------------------编程问答-------------------- 和编码没关系 多线程调用不同代理访问同一个页面 大部分代理都正常,但遇到极少数代理的时候GetRequestStream 就无限超时 --------------------编程问答-------------------- 加个判断。超过多长时间然后在次请求下。 --------------------编程问答-------------------- 现在是超时设置没用 超时时间直接默认成五分钟了,所以有些线程就要等五分钟才抛出异常,这样线程很容易卡死 --------------------编程问答-------------------- 就是明明设置了超时时间 但调用特定代理访问页面的时候超时时间直接默认成五分钟了,不知道咋回事
补充:.NET技术 ,  .NET Framework
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,