asp.net – HttpWebRequest正在为404抛出异常
发布时间:2020-12-15 19:58:17 所属栏目:asp.Net 来源:网络整理
导读:我发现HttpWebRequest正在为不存在的资源抛出WebException. 在我看来非常奇怪,因为HttpWebResponse具有StatusCode属性(存在NotFount项). 你认为它有任何原因,或者它只是开发人员的问题吗? var req = (HttpWebRequest)WebRequest.Create(someUrl);using (Htt
我发现HttpWebRequest正在为不存在的资源抛出WebException.
在我看来非常奇怪,因为HttpWebResponse具有StatusCode属性(存在NotFount项). 你认为它有任何原因,或者它只是开发人员的问题吗? 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获取当前用户名
- asp.net – 调用Web服务提供“无法连接到远程服务器错误”
- asp.net – 绑定TemplateItem中CheckBox的Checked属性
- asp.net-mvc – 是否可以使用自定义错误页面与MVC网站,但不
- 处理global.asax ASP.NET MVC中的异常
- 异常 – ASP.NET应用程序池关闭问题
- razor – MVC 4 ActionLink Dictionary htmlAttributes不起
- asp.net-core – 什么应该是WEB API Action Method的返回类
- asp.net-mvc-3 – 用于Nhibernate事务管理的ActionFilter是
- asp.net – 如何确保使用后无法恢复上传的文件内容?
推荐文章
站长推荐
- asp.net-mvc – 在ASP.NET MVC视图中使用代码隐藏
- asp.net-mvc – 您使用ASP.NET MVC的View Engine
- asp.net-web-api – ASP.NET WebApi测试 – 断言
- ASP.NET Web应用程序本地化的最佳实践
- ASP.Net MVC Cookies不会持续存在
- asp.net-core – 丰富Serlilogs,每个hangfire工作
- asp.net-mvc – MVC DropDownList SelectedValue
- asp.net-mvc – 在内部OnException(ExceptionCon
- 在不刷新ASP.NET网页上的整个页面的情况下刷新内
- 单页应用认证
热点阅读