首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

WebClient卡死解决方案

2014-06-12 
WebClient卡死使用WebClient进行模拟浏览器,但总是出现直接卡死的问题,超时设置了我扩展了基本的WebClient

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")

有时候都会卡死,就是直接卡那儿不动了
把上面放线程里面,直接结束线程也不行
求教大神怎样才能避免卡死?
[解决办法]
试试异步下载DownloadStringAsync,至少不卡界面

热点排行