日期:2014-05-17  浏览次数:21698 次

WebClient卡死
使用WebClient进行模拟浏览器,但总是出现直接卡死的问题,超时设置了
我扩展了基本的WebClient,下面是 关键代码

protected override WebRequest GetWebRequest(Uri address)
        {
            InitHeader();
            Request = (HttpWebRequest)base.GetWebRequest(address);
            Request.MaximumAutomaticRedirections = 1000;
            if (Timeout <= 0)
                Timeout = 60;
            Request.Timeout = Timeout * 1000;
            Request.ReadWriteTimeout = Timeout * 1000;
            Request.AllowAutoRedirect = AllowAutoRedirect;
            Request.CookieContainer = this._cookieContainer;
            Request.KeepAlive = true;
            Request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
            if (address.ToString().StartsWith("https"))
            {
                Credentials = CredentialCache.DefaultNetworkCredentials;
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
                ServicePointManager.ServerCertificateValidationCallback = callback;
            }
            return Request;
        }
protected override WebResponse GetWebResponse(WebRequest request)
        {
            Response = (HttpWebResponse)base.GetWebResponse(request);
            if (Response != null)
            {
                this._cookieContainer.Add(Response.Cookies);
            }
            return Response;
        }


就这样最普通的使用

new WebClientEx().DownloadString("xxxxx")

有时候都会卡死,就是直接卡那儿不动了
把上面放线程里面,直接结束线程也不行
求教大神怎样才能避免卡死?