asp.net – HttpWebRequest正在为404抛出异常
发布时间:2021-01-24 10:26:56 所属栏目: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 webapi 实用的接口加密方法示例
- asp.net-mvc – 缩小ASP.NET MVC中的Action Filter属性
- ASP.NET SQL成员资格表
- 利用ASP.NET MVC和Bootstrap快速搭建个人博客之后台dataTab
- ASP.NET Core 1.0 ConfigurationBuilder().AddJsonFile(“a
- asp.net-mvc – ASP.NET MVC/C++#:可以使用Html.ActionLin
- asp.net – Mocking HttpContext不起作用
- asp.net – 如何查看Chrome开发者工具中发布到表单的数据大
- asp.net-mvc – 为什么我在带有godaddy服务器的MVC3应用程序
- 实体框架 – 等同于.HasOptional在实体框架核心1(EF7)
推荐文章
站长推荐
- asp.net-mvc-3 – 局部视图中的RenderSection
- 序列化 – Newtonsoft中的TypeNameHandling需要$
- asp.net – Intranet / Internet的Windows身份验
- asp.net – __doPostBack在DotNetNuke网站上未定
- asp.net+js 实现无刷新上传解析csv文件的代码
- asp.net-mvc – 从视图到控制器POST信用卡数据是
- asp.net – 如何扩展aspnet成员身份验证表?
- asp.net – 在为app_offline.htm提供特定URL时,将
- asp.net – [DataType(DataType.EmailAddress)]和
- asp.net-mvc – 文件上传MVC
热点阅读
