HttpClient超时时间

  1. 建立连接超时时间
var httpClient = new HttpClient(new SocketsHttpHandler() { ConnectTimeout = ... });
  1. 响应超时时间
httpClient.TimeOut = ....
        services.AddHttpClient("SSR", httpClient =>
        {
            //响应超时时间
            httpClient.Timeout = new System.TimeSpan(0, 0, 15);
        })
        .ConfigurePrimaryHttpMessageHandler(() =>
        {
            return new SocketsHttpHandler()
            {
                //连接超时时间
                ConnectTimeout = new System.TimeSpan(0, 0, 2),

                //忽略证书校验
                SslOptions = new System.Net.Security.SslClientAuthenticationOptions
                {
                    RemoteCertificateValidationCallback = (_, _, _, _) => true
                }
            };
        });

https://docs.microsoft.com/en-us/dotnet/api/system.net.http.socketshttphandler.connecttimeout?view=net-6.0
https://github.com/dotnet/runtime/issues/11594

posted @ 2022-01-17 22:36  ChasingDreams  阅读(163)  评论(0编辑  收藏  举报