C#:底层连接已关闭发送时发生意外错误

C# : the underlying connection was closed an unexpected error occurred on a send

我正在使用必须发送请求以获取身份验证密钥的 API。我尝试了很多方法,但总是遇到如下相同的异常:

the underlying connection was closed an unexpected error occurred on a send.

这是我的代码供参考:

 public AuthTokenReponse GetAuthToken(IClientConfiguration clientConfiguration)
        {
            try
            {
                _logger.Info($"Getting Authentication Token");
                var client = new RestClient(_portalConfiguration.ApiAuthHostUrl);
                client.Proxy = new WebProxy();
                var request = new RestRequest(Method.POST);

                ServicePointManager.Expect100Continue = true;
                ServicePointManager.DefaultConnectionLimit = 9999;
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3;

                request.AddHeader("client_id", clientConfiguration.ApiClientId);
                request.AddHeader("client_secret", clientConfiguration.ApiClientSecret);

                request.AddHeader("Gstin", clientConfiguration.ApiGstin);
                request.AddHeader("user_name", clientConfiguration.ApiUserName);
                request.AddHeader("Content-Type", "application/json; charset=utf-8");
                request.RequestFormat = DataFormat.Json;

                var obj = new AuthRequestDto();
                var appKey = GenerateAppKey();

                obj.Data = new AuthRequestDto.DataDto
                {
                    UserName = clientConfiguration.ApiUserName,
                    Password = EncryptAsymmetric(clientConfiguration.ApiPassword, clientConfiguration.ApiPublicKey),
                    AppKey = Encrypt(appKey, clientConfiguration.ApiPublicKey),
                    ForceRefreshAccessToken = true
                };


                request.AddJsonBody(new
                {
                    data = obj.Data
                });

                _logger.Error($"Request Json : {JsonConvert.SerializeObject(request)}");


                IRestResponse response = client.Execute(request);

                //Here I'm getting the error in my Log file.
                _logger.Error($"Response Error {response.ErrorMessage}");
                _logger.Error($"Response Content {response.Content}");

                if (response != null && response.StatusCode == HttpStatusCode.OK)
                {
                    var result = JsonConvert.DeserializeObject<AuthTokenReponse>(response.Content);

                    if (result.Status > 0)
                    {
                        result.SekToken = DecryptBySymmetricKey(result.Data.Sek, appKey);

                        return result;
                    }
                    _logger.Error($"Error while getting auth token {response.Content} {response.StatusCode}");
                }
                else if (response != null)
                {
                    _logger.Error($"Error while getting auth token {response.Content} Status Code: {response.StatusCode}");
                }
                else
                {
                    _logger.Error($"Error while getting auth token {response.Content} , Reason unknown.");

                }



                return null;

            }
 catch (Exception ex)
            {
                _logger.Error($"Error while getting auth token", ex);

                return null;
            }

        }

我已经尝试过的选项:

  1. 清除缓存
  2. 添加安全协议

如果您有任何解决方案,请告诉我。

一切都很好,客户端的服务器有一个限制,即调用 API 时使用的每个 IP 都需要在他们的门户上注册。出现错误是因为 IP 未注册。

所以如果有人遇到类似的问题,那么也请尝试这个解决方案。