HttpWebRequest的是404抛出异常 [英] HttpWebRequest is throwing exception for 404

查看:79
本文介绍了HttpWebRequest的是404抛出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现的HttpWebRequest抛出引发WebException不存在的资源。
在我看来很奇怪,因为HttpWebResponse有状态code属性(NotFount项目存在)。
你认为它有任何理由或者它仅仅是开发商的问题?

  VAR REQ =(HttpWebRequest的)WebRequest.Create(someUrl);
使用(HttpWebResponse响应=(HttpWebResponse)req.GetResponse()){
    如果(response.Status code ==的HTTPStatus code.OK){...}
}


解决方案

这确实是一个令人沮丧的问题,可以通过周围使用下面的扩展方法类,并调用request.BetterGetResponse()来制定

  // ------------------------------------ -----------------------------------
//
//版权所有(C)2011加勒特Serack。版权所有。
//
//
//该软件是在Apache 2.0授权(以下简称许可证)
//您不得使用该软件只有在符合许可。
//
// ------------------------------------------------ -----------------------命名空间CoApp.Toolkit.Extensions {
    使用系统;
    使用System.Net;    公共静态类WebRequestExtensions {
        公共静态WebResponse类BetterEndGetResponse(这WebRequest的要求,IAsyncResult的asyncResult){
            尝试{
                返回request.EndGetResponse(asyncResult);
            }
            赶上(引发WebException WEX){
                如果(wex.Response!= NULL){
                    返回wex.Response;
                }
                扔;
            }
        }        公共静态WebResponse类BetterGetResponse(此要求的WebRequest){
            尝试{
                返回request.GetResponse();
            }
            赶上(引发WebException WEX){
                如果(wex.Response!= NULL){
                    返回wex.Response;
                }
                扔;
            }
        }
    }
}

您阅读我的博客文章更多关于它在上<一这个主题href=\"http://fearthecowboy.com/2011/09/02/fixing-webrequests-desire-to-throw-exceptions-instead-of-returning-status/\" rel=\"nofollow\">http://fearthecowboy.com/2011/09/02/fixing-webrequests-desire-to-throw-exceptions-instead-of-returning-status/

I've found that HttpWebRequest is throwing WebException for not existing resources. It seems to me very strange as HttpWebResponse has StatusCode property (NotFount item exist). Do you think it has any reasons for that or maybe it is just developers issue?

var req = (HttpWebRequest)WebRequest.Create(someUrl);
using (HttpWebResponse response = (HttpWebResponse)req.GetResponse()) {
    if (response.StatusCode == HttpStatusCode.OK) { ...}
}

解决方案

This is really a frustrating problem, which can be worked around by using the following extension method class and calling 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;
            }
        }
    }
}

You read more about it in my blog post on this subject at http://fearthecowboy.com/2011/09/02/fixing-webrequests-desire-to-throw-exceptions-instead-of-returning-status/

这篇关于HttpWebRequest的是404抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆