asp.net – HttpWebRequest正在为404抛出异常
发布时间:2021-01-24 09:12:35 所属栏目: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-mvc – 使用AWS .NET SDK进行SNS订阅确认示例
- asp.net – Umbraco CMS(.NET):加载xslt /用户控件的日志错
- asp.net中的GridView分页问题
- asp.net – 如果我没有指定targetFramework =“4.0”会发生
- 如何单元测试我的asp.net-mvc控制器的OnActionExecuting方法
- asp.net-mvc – 不要在ASP .NET MVC 4 BundleConfig中缩小某
- asp.net-mvc – 当注入服务或控制器的依赖关系太多时,重构策
- 如何使用ASP.NET MVC Web API OData链接到Razor中的OData集
- asp.net – Request.Url.AbsoluteUri和重写的URL
- 在ASP.NET MVC4中是否可以将C#或VB函数标记为Javascript?
推荐文章
站长推荐
- asp.net-mvc – MVC会话过期而不是身份验证
- ASP.NET Core中调整HTTP请求大小的几种方法详解
- 在asp.net c#应用程序中使用Graphviz Dll
- asp.net – IControllerFactory’MyWebSite.WebU
- asp.net-mvc – 如何将MVC 5 IdentityModels.cs移
- asp.net-mvc – 从基本控制器继承LINQ-to-SQL数据
- asp.net-mvc – 存储库与DAL中的服务模式:EF和D
- 勾选复选框时,禁用一些ASP.Net验证控件
- ASP.NET学习CORE中使用Cookie身份认证方法
- asp.net – 小数点后尾数为零
热点阅读