问题描述
本人有段WebRequest使用HTTP/HTTPS代理请求页面的代码,代码已经做了超时设置,对使用大部分带来请求数据的时候超时设置都起作用,但是在使用很少部分HTTPS代理的时候,超时设置根本不请任何作用,WebRequest.GetRequestStream无限等待,不知道为什么会出现这种情况。请大家帮下忙stringhtmlResult="";try{ServicePointManager.Expect100Continue=false;HttpWebRequestrequest=null;if(strURL.StartsWith("https",StringComparison.OrdinalIgnoreCase)){ServicePointManager.ServerCertificateValidationCallback=newRemoteCertificateValidationCallback(CheckValidationResult);request=WebRequest.Create(strURL)asHttpWebRequest;}else{request=WebRequest.Create(strURL)asHttpWebRequest;}request.MaximumAutomaticRedirections=5;request.Timeout=maxReadMs;request.ReadWriteTimeout=maxReadMs;request.AllowAutoRedirect=redirect302;if(proxy!=null&&!string.IsNullOrEmpty(proxy.Host)){request.Proxy=newWebProxy(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;MSIE9.0;WindowsNT6.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+="rn";MatchCollectionheads=newRegex(".*?rn").Matches(appendHttpHeads);foreach(Matchheadinheads){if(!string.IsNullOrEmpty(head.Value.Trim()))request.Headers.Add(head.Value.Trim());}}if(cookie!=null){request.CookieContainer=newCookieContainer();request.CookieContainer.Add(cookie.ToCookieCollection());}try{if(!string.IsNullOrEmpty(postData)){byte[]data=Encoding.UTF8.GetBytes(postData);request.ContentLength=data.Length;using(Streamstream=request.GetRequestStream()){stream.Write(data,0,data.Length);}}using(WebResponseresponse=(HttpWebResponse)request.GetResponse()){Streamstream=response.GetResponseStream();if(response.Headers[HttpResponseHeader.ContentEncoding]!=null&&response.Headers[HttpResponseHeader.ContentEncoding].ToLower().Contains("gzip"))stream=newGZipStream(stream,CompressionMode.Decompress);elseif(response.Headers[HttpResponseHeader.ContentEncoding]!=null&&response.Headers[HttpResponseHeader.ContentEncoding].ToLower().Contains("deflate"))stream=newDeflateStream(stream,CompressionMode.Decompress);stringcharset="";if(!string.IsNullOrEmpty(response.ContentType)){if(newRegex("charset=",RegexOptions.IgnoreCase).IsMatch(response.ContentType))charset=newRegex("charset=.*?[;|]",RegexOptions.IgnoreCase).Match(response.ContentType+"").Value.Split('=')[1].Trim().Trim(';');}if(string.IsNullOrEmpty(charset))charset="utf-8";StreamReaderreader=newStreamReader(stream,Encoding.GetEncoding(charset));htmlResult=reader.ReadToEnd();strURL=response.ResponseUri.ToString();referer=strURL;stringcookiesHeaders=response.Headers[HttpResponseHeader.SetCookie];if(!string.IsNullOrEmpty(cookiesHeaders))cookie.Add(cookiesHeaders,response.ResponseUri.Host);reader.Close();reader.Dispose();stream.Dispose();}}catch(WebExceptione){using(WebResponseresponse=e.Response){if(response!=null){HttpWebResponsehttpResponse=(HttpWebResponse)response;switch(httpResponse.StatusCode){caseHttpStatusCode.RequestTimeout:break;default:break;}}}}}catch{}returnhtmlResult;
解决方案
解决方案二:
请求的数据编码是什么?要和发送的request.ContentType="application/x-www-form-urlencoded";编码一致
解决方案三:
和编码没关系多线程调用不同代理访问同一个页面大部分代理都正常,但遇到极少数代理的时候GetRequestStream就无限超时
解决方案四:
加个判断。超过多长时间然后在次请求下。
解决方案五:
现在是超时设置没用超时时间直接默认成五分钟了,所以有些线程就要等五分钟才抛出异常,这样线程很容易卡死
解决方案六:
就是明明设置了超时时间但调用特定代理访问页面的时候超时时间直接默认成五分钟了,不知道咋回事