asp.net – HttpWebRequest正在为404抛出异常
发布时间:2021-01-24 14:03:48 所属栏目: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 – 未在ELMAH中记录的错误
- asp.net – 在部分视图中强制使用没有Html.BeginForm / Aja
- asp.net – HttpWebRequest正在为404抛出异常
- .net – 有人有一个例子,说明为什么我会主持一个WCF服务
- asp.net-mvc – 找到相同类型的两个实体之间的差异
- Asp.NET控制文件上传的大小方法(超简单)
- asp.net-mvc – DDD原理和ASP.NET MVC项目设计
- asp.net中让Repeater和GridView支持DataPager分页
- asp.net+js 实现无刷新上传解析csv文件的代码
- ASP.NET 2.0和4.0似乎在Forms身份验证中以不同方式处理根UR
推荐文章
站长推荐
- asp.net – 如何在Visual Studio中添加NUnit
- Phonegap上的ASP.NET窗体身份验证问题(Android)
- asp.net – 在剃刀中等同于End / Response.End?
- Asp.net 文件上传类(取得文件后缀名,保存文件,加
- asp.net-web-api – ASP身份OAuth令牌 – 我应该
- ASP.NET Core使用SkiaSharp实现验证码的示例代码
- asp.net 汉字转换拼音及首字母实现代码
- asp.net – 为什么HttpContext.Current.User.Ide
- 在asp.net中使用加密数据库联接字符串保证数据安
- asp.net-mvc – ASP.NET MVC模型/ ViewModel验证
热点阅读