asp.net – HttpWebRequest正在为404抛出异常
发布时间:2021-01-24 10:12:36 所属栏目: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超大文件上传问题解决
- asp.net-mvc – 什么是应用程序洞察遥测(未配置)在做什么?
- asp.net-mvc-3 – 在F#中的ViewBag动态对象上设置属性
- asp.net-mvc – AntiXss保护Html模型属性
- asp.net-mvc – 在Controller操作方法中重用代码的最佳方法
- asp.net-mvc – WebService还是一个简单的MVC控制器?
- asp.net-mvc – asp.net MVC antiorgerytoken异常RedirectT
- asp.net – 请求在IIS工作进程中存在于RequestAcquireState
- asp.net-mvc – ASP.NET MVC/C++#:可以使用Html.ActionLin
- asp.net-mvc – UpdateModel前缀 – ASP.NET MVC
推荐文章
站长推荐
- asp.net-mvc – SSL安全SaaS应用程序的URL设计
- asp.net-web-api – 在WebApi OData中为OData服务
- 在asp.net mvc中如何使用usercontrols来显示“岛
- 解决asp.net Sharepoint无法连接发布自定义字符串
- asp.net-mvc – ASP.net身份在删除外部帐户后停止
- Asp.net核心IIS Express.如何查看日志消息?
- asp-classic – 从Classic ASP执行存储过程
- asp.net-mvc – ASP.NET MVC Beta 1:DefaultMod
- asp.net-mvc-3 – dataannotations在主键上设置标
- asp.net-mvc-3 – 在MVC Razor View中使用If语句
热点阅读
