asp.net – HttpWebRequest正在为404抛出异常
发布时间:2021-01-24 13:14:03 所属栏目:asp.Net 来源:互联网
导读:我发现HttpWebRequest正在为不存在的资源抛出WebException. 在我看来非常奇怪,因为HttpWebResponse具有StatusCode属性(存在NotFount项). 你认为它有任何原因,或者它只是开发人员的问题吗? var req = (HttpWebRequest)WebRequest.Create(someUrl);using (Http
我发现HttpWebRequest正在为不存在的资源抛出WebException.
var req = (HttpWebRequest)WebRequest.Create(someUrl); using (HttpWebResponse response = (HttpWebResponse)req.GetResponse()) { if (response.StatusCode == HttpStatusCode.OK) { ...} } 解决方法这确实是一个令人沮丧的问题,可以通过使用以下扩展方法类并调用request.BetterGetResponse()来解决这个问题.//----------------------------------------------------------------------- // // Copyright (c) 2011 Garrett Serack. All rights reserved. // // // The software is licensed under the Apache 2.0 License (the "License") // You may not use the software except in compliance with the License. // //----------------------------------------------------------------------- namespace CoApp.Toolkit.Extensions { using System; using System.Net; public static class WebRequestExtensions { public static WebResponse BetterEndGetResponse(this WebRequest request,IAsyncResult asyncResult) { try { return request.EndGetResponse(asyncResult); } catch (WebException wex) { if( wex.Response != null ) { return wex.Response; } throw; } } public static WebResponse BetterGetResponse(this WebRequest request) { try { return request.GetResponse(); } catch (WebException wex) { if( wex.Response != null ) { return wex.Response; } throw; } } } } 你在http://fearthecowboy.com/2011/09/02/fixing-webrequests-desire-to-throw-exceptions-instead-of-returning-status/关于这个主题的博客文章中阅读了更多关于它的内容 (编辑:莱芜站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- ASP.NET MembershipProvider加密/解密
- .net – 什么可以解释托管堆上超过5,000,000个System.WeakR
- asp.net-mvc – ASP.NET MVC 3 Treeview
- 在mvc4 asp.net中的Razor View中的模型声明
- asp.net-mvc-4 – 在asp.net mvc 4模型中更改验证触发顺序
- asp.net-mvc – 如何在扩展方法中使用HTML帮助器方法?
- 如何保护我的ASP.NET AJAX应用程序?
- Asp.Net超大文件上传问题解决
- asp.net-mvc – ASP.NET MVC应用程序的WatiN最佳实践/经验法
- asp.net-mvc – 什么是应用程序洞察遥测(未配置)在做什么?
推荐文章
站长推荐
- asp.net通过Ajax UpdatePanel回传后滚动条位置变
- asp.net简单生成XML文件的方法
- asp.net-mvc – 使用Repository/Service Pattern
- asp.net-mvc – 路由是在我的区域中找到控制器,但
- asp.net – VS插件:查看标记.存在这样的事情吗?
- asp.net-mvc-4 – 如何在Kendo UI Grid中扩展页面
- asp.net-mvc – MicrosoftMvcValidation.js VS j
- Asp.net 实现Session分布式储存(Redis,Mongodb,M
- 从ASP.NET C#启动一个程序
- ASP.Net 分页控件源码
热点阅读