c# – WebClient.DownloadString(url)当这个url返回404页面时,我
发布时间:2020-12-16 00:02:28 所属栏目:百科 来源:网络整理
导读:我正在使用WebClient.DownloadString(url)下载一个网页,当一个网址停止并且不再起作用的404网页时. 当我遇到这个错误时,我想跳过这些页面. 如果网址是404页面,则不会开始下载.所以我无法解析未下载的数据…… 解决方法 您必须捕获异常并测试404: try{ strin
我正在使用WebClient.DownloadString(url)下载一个网页,当一个网址停止并且不再起作用的404网页时.
当我遇到这个错误时,我想跳过这些页面. 如果网址是404页面,则不会开始下载.所以我无法解析未下载的数据…… 解决方法
您必须捕获异常并测试404:
try { string myString; using (WebClient wc = new WebClient()) myString= wc.DownloadString("http://foo.com"); } catch (WebException ex) { if (ex.Status == WebExceptionStatus.ProtocolError && ex.Response != null) { var resp = (HttpWebResponse)ex.Response; if (resp.StatusCode == HttpStatusCode.NotFound) // HTTP 404 { //the page was not found,continue with next in the for loop continue; } } //throw any other exception - this should not occur throw; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |