鸿 网 互 联 www.68idc.cn

当前位置 : 服务器租用 > .net技术 > winform编程 > >

c#中网络异常的处理办法

来源:互联网 作者:佚名 时间:2013-12-28 10:44
加入try catch来判断,catch使用的 WebException 来处理 try { var request = WebRequest.Create(uri); using ( var response = request.GetResponse()){ using ( var responseStream = response.GetResponseStream()){ // Process the stream }}} catch (We

加入try catch来判断,catch使用的WebException来处理

try { var request = WebRequest.Create(uri); using (var response = request.GetResponse()) { using (var responseStream = response.GetResponseStream()) { // Process the stream } } } catch (WebException ex) { if (ex.Status == WebExceptionStatus.ProtocolError && ex.Response != null) { var resp = (HttpWebResponse) ex.Response; if (resp.StatusCode == HttpStatusCode.NotFound) { // Do something } else { // Do something else } } else { // Do something else } }

网友评论
<