HttpClient返回异常分类

当遇到某一类异常不进行重试

Code

var polly = Policy.Handle<Exception>(ex => NeedHandlerRetryException(ex)).WaitAndRetryAsync(2,
                    retryAttempt => TimeSpan.FromSeconds(2)
                 );

private bool NeedHandlerRetryException(Exception ex)
{
    if(ex.StatusCode == 0)
    {
        var actualException = ex.InnerException;
        if(actualException  != null)
        {                
            if(actualException is TimeoutException ||(actualException is WebException webEx && webEx.Statu== WebExceptionStatuNameResolutionFailure)) //连时 || 域名解析失败
            {
                return false;
            }
            if(actualException is SocketException)//网络中断
            {
                return true;
            }
            var retryExceptions = ne WebExceptionStatus[]
            {
                WebExceptionStatuConnectFailure,
                WebExceptionStatuConnectionClosed,
                WebExceptionStatuRequestCanceled
            };
            if (actualException is WebException webException &webException.Status.Is(retryExceptions))//连接失败,是需要其他status?如果不需要,该方法只返回false的场景
            {
                return true;
            }
            
        }
    }
    return true;
}

// 摘要:
//     Defines status codes for the System.NetWebException class.
public enum WebExceptionStatus
{
    //
    // 摘要:
    //     No error was encountered.
    Success = 0,
    //
    // 摘要:
    //     The name resolver service could notresolve the host name.
    NameResolutionFailure = 1,
    //
    // 摘要:
    //     The remote service point could not becontacted at the transport level.
    ConnectFailure = 2,
    //
    // 摘要:
    //     A complete response was not receivedfrom the remote server.
    ReceiveFailure = 3,
    //
    // 摘要:
    //     A complete request could not be sent tothe remote server.
    SendFailure = 4,
    //
    // 摘要:
    //     The request was a pipelined request andthe connection was closed before the
    //     response was received.
    PipelineFailure = 5,
    //
    // 摘要:
    //     The request was canceled, the SystemNet.WebRequest.Abort method was called,
    //     or an unclassifiable error occurred.This is the default value for System.NetWebException.Status.
    RequestCanceled = 6,
    //
    // 摘要:
    //     The response received from the serverwas complete but indicated a protocol-level
    //     error. For example, an HTTP protocolerror such as 401 Access Denied would use
    //     this status.
    ProtocolError = 7,
    //
    // 摘要:
    //     The connection was prematurely closed.
    ConnectionClosed = 8,
    //
    // 摘要:
    //     A server certificate could not bevalidated.
    TrustFailure = 9,
    //
    // 摘要:
    //     An error occurred while establishing aconnection using SSL.
    SecureChannelFailure = 10,
    //
    // 摘要:
    //     The server response was not a validHTTP response.
    ServerProtocolViolation = 11,
    //
    // 摘要:
    //     The connection for a request thatspecifies the Keep-alive header was closed
    //     unexpectedly.
    KeepAliveFailure = 12,
    //
    // 摘要:
    //     An internal asynchronous request ispending.
    Pending = 13,
    //
    // 摘要:
    //     No response was received during thetime-out period for a request.
    Timeout = 14,
    //
    // 摘要:
    //     The name resolver service could notresolve the proxy host name.
    ProxyNameResolutionFailure = 15,
    //
    // 摘要:
    //     An exception of unknown type hasoccurred.
    UnknownError = 16,
    //
    // 摘要:
    //     A message was received that exceededthe specified limit when sending a request
    //     or receiving a response from the server.
    MessageLengthLimitExceeded = 17,
    //
    // 摘要:
    //     The specified cache entry was not found.
    CacheEntryNotFound = 18,
    //
    // 摘要:
    //     The request was not permitted by thecache policy. In general, this occurs when
    //     a request is not cacheable and theeffective policy prohibits sending the request
    //     to the server. You might receive thisstatus if a request method implies the
    //     presence of a request body, a requestmethod requires direct interaction with
    //     the server, or a request contains aconditional header.
    RequestProhibitedByCachePolicy = 19,
    //
    // 摘要:
    //     This request was not permitted by theproxy.
    RequestProhibitedByProxy = 20
}

参考地址

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值