asp.net – HttpWebRequest正在为404抛出异常
发布时间:2021-01-24 11:27:15 所属栏目: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 – IIS 404自定义错误不能按预期工作
- asp.net类序列化生成xml文件实例详解
- asp.net – 我应该使用WebMatrix构建一个真实世界的网站吗?
- linq – ASP.NET Web API GET方法:为单个参数传递多个值
- asp.net-mvc – 根据浏览器接受语言自动设置uiCulture
- asp.net-mvc – 在ClaimsIdentity上,BootstrapContext为nul
- Asp.Net 5分钟实现网页实时监控
- asp.net-mvc – ASP.NET MVC如何实现返回上一页的链接?
- 在ASP.Net中防止SQL注入
- asp.net – WebForm_DoCallback定义
推荐文章
站长推荐
- asp.net – 我可以在超链接上显式指定NavigateUr
- ASP.Net 2中的上传文件在哪里?
- asp.net-mvc – ControllerActionInvoker
- asp.net – 如何使用resxresourcewriter写入所有
- ASP.NET MVC是否使Web表单成为旧版平台?
- 从ASP.Net中的sessionID获取会话对象
- ASP.NET学习CORE中使用Cookie身份认证方法
- ASP.NET UpdatePanel和Javascript __dopostback
- asp.net-mvc – 使用IIS基本身份验证的OWIN身份验
- asp.net – 转发器控件中的单选按钮列表
热点阅读