asp.net – HttpWebRequest正在为404抛出异常
发布时间:2020-05-23 01:39:39 所属栏目: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 – 在.NET 4.5中混合使用Windows和Forms身份验证:
- asp.net – 如何直接在.aspx页面中访问web.config设置?
- 将额外的信息与ASP.NET MVC成员关联
- asp.net-mvc – SessionStateTempDataProvider要求启用Sess
- asp.net下文件上传和文件删除的代码
- asp.net – 如何在加载/性能测试期间找到IIS要模拟的平均并
- asp.net-identity – 在调用SignIn后,可以通过OWIN修改ASP.
- 处理global.asax ASP.NET MVC中的异常
- asp.net 读取并修改config文件实现代码
- ASP.NET中的应用程序生存期
推荐文章
站长推荐
- 为什么私有事件处理程序在ASP.NET中不起作用
- asp.net-web-api – 更好地总是在Web Api中返回H
- 如何在ASP.NET生成的Word文件中嵌入图像
- asp.net-mvc – 将EF 4.1代码首先将ASP.NET MVC3
- asp.net-mvc – 如何根据设备类型更改ASP.NET MV
- asp.net-mvc-4 – 如何在MVC 4中创建自定义WebSe
- asp.net-mvc – jqGrid和MVC3 – 添加模型验证
- asp.net – Javascript日期本地化
- asp.net – 具有html5中的文本以外的输入类型的U
- 如何使用ASP.NET在纯HTML页面上执行表单验证?
热点阅读
